Using a list of names to delete bookmarks in a Word Doc

m_in_spain

Board Regular
Joined
Sep 28, 2018
Messages
72
Office Version
  1. 365
Platform
  1. Windows
OK, here we go! This one (appears to me) to be more difficult.

I have a dynamic list with no blanks which i create in Column BY, like this: (thanks to this forum for parts of this)
step one deete any current Nameslike this:

Code:
Sub DeleteNames()  Dim Cell As Range
  On Error Resume Next
  For Each Cell In Range("BY3:BY100")
    Cell.Name.Delete
  Next
  On Error GoTo 0
End Sub

Next I populate the Names from the new list like this:

Code:
Sub makenames()
    Application.DisplayAlerts = False
    Range("BY3:BZ100").Select
   Selection.CreateNames Top:=False, Left:=False, Bottom:=False, Right:= _
        True
    Application.DisplayAlerts = True
End Sub

Now my question.

I want to take each of those "Names" and search through my Word document for the same name and delete it, if it exists.

I can do it manually by using:

Code:
docWord.bookmarks("XXXxxxXXX").Select
       .Selection.Delete

what i want to do is loop through all the names and replace the XXXxxxXXX

As ever, any ideas will be looked at, tried out but ALL will be thanked.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Code:
Dim Cell As Range
For Each Cell In Range("BY3:BY100")
  docWord.bookmarks(Cell.Name).Delete
Next
 
Upvote 0
Presumably, that's because one or more or the bookmarks don't exist. You can work around that with 'On Error Resume Next' or with:
If docWord.Bookmarks.Exists(Cell.Name) Then docWord.Bookmarks(Cell.Name).Delete
 
Upvote 0
Many thanks.
That works and deletes the text i have within the same name bookmark in word.
It seems to paste the name of my bookmark into the word document where it removed the original text.
 
Upvote 0
It seems to paste the name of my bookmark into the word document where it removed the original text.
The code I posted doesn't insert the names into the document; they were already there, at the bookmarked locations. If you want to delete those, too, use:
Code:
With docWord
  If .Bookmarks.Exists(Cell.Name) Then
    With .Bookmarks(Cell.Name)
      .Range.Text = vbNullString
      .Delete
    End With
  End If
End With
 
Upvote 0

Forum statistics

Threads
1,223,738
Messages
6,174,213
Members
452,551
Latest member
croud

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