rename bookmarks with code

J

Jesper F

How can go through the bookmarks in a document and rename them all?
I have 10-100 bookmarks that are named q1, q2, q3 etc.

Can I iterate through them and rename them?
I get and error saying the .name property is read only.
 
G

Greg Maxey

Jesper,

I don't think that it is possible to rename a bookmark with code. What you
can do however is redefine the the bookmark with a different name.
Something like:

Sub ReDefineBookmarks()
Dim bkmCol As New Collection
Dim oBkm As Bookmark
Dim colElement
Dim Count As Integer

Count = 0
For Each oBkm In ActiveDocument.Bookmarks
bkmCol.Add oBkm.Name
Next
With ActiveDocument.Bookmarks
For Each colElement In bkmCol
Count = Count + 1
.Add "NewName" & Count, .Item(colElement).Range
.Item(colElement).Delete
Next
End With
End Sub
 
G

Greg Maxey

I suppose that you could use something like this if you wanted to assign a
unique name to each redefined macro:

Sub ReDefineBookmarks()
Dim bkmCol As New Collection
Dim oBkm As Bookmark
Dim colElement
Dim Count As Integer
Dim CurName As String
Dim NewName As String

Count = 0
For Each oBkm In ActiveDocument.Bookmarks
bkmCol.Add oBkm.Name
Next
With ActiveDocument.Bookmarks
For Each colElement In bkmCol
Count = Count + 1
.Item(colElement).Range.Select
CurName = .Item(colElement).Name
NewName = InputBox("Current Name: " & CurName & vbCr & "New name: ")
If NewName <> "" Then
.Add NewName, .Item(colElement).Range
.Item(colElement).Delete
End If
Next
End With
End Sub
 
H

Helmut Weber

Hi Jesper, hi Greg:

.... or like this:

Sub RenameBookmark(n1$, n2$)
Dim rTmp As Range
With ActiveDocument.Range
Set rTmp = .Bookmarks(n1$).Range
.Bookmarks(n1$).Delete
.Bookmarks.Add Name:=n2$, Range:=rTmp
End With
End Sub
' ---
Sub test()
RenameBookmark "Bookmark1", "Bookmark2"
End Sub

You may check whether the bookmark exists beforehand.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
J

Jesper F

I suppose that you could use something like this if you wanted to assign a
unique name to each redefined macro:

Thanks a lot Greg, I'll try these out. Thanks!


Jesper, Denmark
 

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