Re: Rename all bookmarks by Martin
Martin
Fri Dec 12 09:07:09 CST 2003
Hi Steve
> I assume the macro would use the line "For Each
> objBookmark In ActiveDocument.Bookmarks" but I cannot
> work out the string manipulation functions to add the
> extra X_ to the front of the bookmark name. Could anyone
> help?
Since you cannot rename bookmarks, you have to use another
approach, e.g. the following:
1. read in all bookmark names into a collection
2.a) add new bookmark for each name in the collection
2.b) delete the old bookmark
Here's a sub which does exactly this:
Sub PrefixBookmarks(prefix As String)
Dim col As New Collection ' new is important, here!!!
Dim oBM As Bookmark
Dim el
' first loop: gather information
For Each oBM In ActiveDocument.Bookmarks
' only consider this bookmark, if it
' does not already have been prefixed
If Not oBM.name Like prefix & "*" Then
col.Add oBM.name
End If
Next
' second loop: add new bookmarks and delete old ones
With ActiveDocument.Bookmarks
For Each el In col
' add new bookmark using original range
.Add "X_" & el, .Item(el).Range
' remove old bookmark
.Item(el).Delete
Next
End With
End Sub
Sub PrefixBookmarksTest()
Call PrefixBookmarks("X_")
End Sub
Cheers,
Martin