Word VBA Macro to Remove All Bookmarks in a Document
This macro removes all the bookmarks from a Word (2007) Document. I use it to update documents which are imported from Framemaker and had a lot of unused bookmarks.
It may not be the most efficient code, but it does work.
The approach is to create an array of bookmark names or ids, and then loop through and remove them.
Note the ShowHidden, which ensures the entire collection is accessible.
Sub ClearBookmarks()
'
' ClearBookmarks Macro
'
'
Dim I, L, Bees() As String, aBookmark
ActiveDocument.Bookmarks.ShowHidden = True
MsgBox "There are " + Str(ActiveDocument.Bookmarks.Count) + " bookmarks"
I = 1
L = ActiveDocument.Bookmarks.Count
ReDim Bees(L)
For Each aBookmark In ActiveDocument.Bookmarks
If IsObject(aBookmark) Then
On Error GoTo Skip
Bees(I) = aBookmark.Name
I = I + 1
End If
Skip:
Next aBookmark
I = LBound(Bees)
L = UBound(Bees)
For I = I To L
If Bees(I) <> "" Then
If ActiveDocument.Bookmarks.Exists(Name:=Bees(I)) Then
ActiveDocument.Bookmarks(Index:=Bees(I)).Delete
End If
End If
Next
MsgBox "There are " + Str(ActiveDocument.Bookmarks.Count) + " bookmarks"
End Sub
The Bees array is very busy. :)
Print article | This entry was posted by elvis on 12/04/10 at 03:40:43 pm . Follow any responses to this post through RSS 2.0. |