VBA - Delete and insert bookmark text in same word document based on excel cell values

HH7

New Member
Joined
Jun 15, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I found this post message 3566 by Macropod advising this macro deletes a bookmark when the value is ‘0’, but when the value is ‘1’ can you then insert a name & address for example into that said bookmark in the same macro?
E.g. .Bookmarks (“Name”).Range.Text=ws.Range(“B2”).Value

Sub CreateWordDocument()
'Note: This code requires a VBA reference to Word, via Tools|References
Dim xlSht As Worksheet, wdApp As New Word.Application, wdDoc As Word.Document, lRow As Long, r As Long
Set xlSht = ActiveSheet: lRow = xlSht.UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row
With wdApp
'Show the Word session
.Visible = True
' create a new Word Document based on the specified template
Set wdDoc = .Documents.Add("C:\MyTemplate.dotm")
With wdDoc
'loop through used range and process the corresponding Word bookmarks
For r = 1 To lRow
If .Bookmarks.Exists(xlSht.Range("A" & r).Text) Then
'Erase designated bookmark content
If xlSht.Range("B" & r).Value = 0 Then
.Bookmarks(xlSht.Range("A" & r).Text).Range.Text = vbNullString
End If
End If
Next
End With
.Activate
End With
'MEMORY CLEANUP
Set wdDoc = Nothing: Set wdApp = Nothing: Set xlSht = Nothing
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,223,705
Messages
6,173,985
Members
452,540
Latest member
haasro02

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top