Rename all bookmarks

S

Steve Wylie

I need a short macro that will rename every bookmark in
the active document by adding "X_" to the beginning of
the name. For example, if a bookmark is called OS121203a
it will rename it X_OS121203a. It will do this for every
bookmark in the current document.

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?

Thanks
Steve
 
M

Martin Seelhofer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top