Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename Bookmarks in Word doc

Status
Not open for further replies.

Jofx

Technical User
Jan 12, 2005
16
FR
Is it possible to rename a set of bookmarks with vba script ?

ex: my doc contains several bookmarks like:

[BMK_1]
[BMK_2]
[BMK_3]

I'd like to rename them to:

[NEW_BMK_1]
[NEW_BMK_2]
[NEW_BMK_3]

without deleting and inserting new bookmarks
Thanks
 
Not directly, no.
"Name" is a readonly property of the bookmark collection.

You could however cheat MS Word by
a) Saving your document in RichText Format
b) opening in a suitable text editor, e.g. Notepad++
c) search/replace these thingies:
Code:
{\*\bkmkstart BMK_1}...
and
Code:
{\*\bkmkend BKM_1}

That can also be done via code & regular expressions.

;-)

Cheers,
MiS

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Hi Jofx,

Is there a reason for not deleting & recreating the bookmarks?


Cheers
[MS MVP - Word]
 
Like this:
Code:
Sub RenameBookmarks()
'  although it does NOT rename them
'  it creates a new one for the same range
'  then deletes the old one

Dim BM_Names()
Dim j As Long
Dim var
Dim strStart As String

For var = 1 To ActiveDocument.Bookmarks.Count
   strStart = ActiveDocument.Bookmarks(var).Name
   ReDim Preserve BM_Names(j)
   BM_Names(j) = strStart
   With ActiveDocument
      .Bookmarks.Add Name:="NEW_" & _
         strStart, _
         Range:=ActiveDocument.Bookmarks(var).Range
   End With
   j = j + 1
Next

For var = 0 To UBound(BM_Names())
   ActiveDocument.Bookmarks(BM_Names(var)).Delete
Next
End Sub
You need to separate the creation of the new bookmarks from the deletion of the old. Why? Because of the way Word handles the bookmarks collection (alphabetically).

You can not really use a For Each and a bookmark object, because every time you create the new bookmark it gets included in the For Each. And if you do a Count like:
Code:
Dim j As Long
Dim var
Dim strStart As String

For var = 1 To ActiveDocument.Bookmarks.Count
   strStart = ActiveDocument.Bookmarks(var).Name
   With ActiveDocument
      .Bookmarks.Add Name:="NEW_" & _
         strStart, _
         Range:=ActiveDocument.Bookmarks(strStart).Range
   End With
   ActiveDocument.Bookmarks(strStart).Delete
Next
you get the result of:

BKM_1 (index = 1)
BKM_2 (index = 2)

First iteration removes BKM_1, and puts in NEW_BKM_1, thus the collection is:

BKM_2 (index = 1)
NEW_BKM_1 (index = 2)

because Word looks at the collection alphabetical, thus when var = 2
Code:
strStart = ActiveDocument.Bookmarks(var).Name
strStart = "NEW_BKM_1", and thus it becomes....

NEW_NEW_BKM_1

So the first code, creates new bookmarks right on top of the old bookmarks; builds an array of the old bookmarks names, and then deletes the bookmarks with those names. This by-passes any issues with alphabetical names and locations within the collection.

Gerry
 
Alternatively:
Code:
Sub RenameBookmarks()
'  although it does NOT rename them
'  it creates a new one for the same range
'  then deletes the old one

Dim BM_Names()
Dim i As Long
With ActiveDocument
  For i = 1 To .Bookmarks.Count
    ReDim Preserve BM_Names(i)
    BM_Names(i) = .Bookmarks(i).Name
  Next
  For i = 1 To .Bookmarks.Count
    With .Bookmarks(BM_Names(i))
      .Range.Bookmarks.Add Name:="NEW_" & .Name, Range:=.Range
      .Delete
    End With
  Next
End With
End Sub


Cheers
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top