Word – Batch rename multiple bookmarks in Word .docx file

docxmicrosoft wordmicrosoft-officeopenoffice

Is it possible to rename multiple bookmarks in standard Word 97-2003 doc file? I just finished a 400 page document, in Word 2010, with several hundred bookmarks that must be renamed and I definitely don't want to go in and rename them manually. I found this Word add-in for enhancing the built-in Word Bookmark Dialog, but can't seem to get it to load properly into my document, if I can get it working it will solve my problem. Until then are there any other straightforward methods for accomplishing this? Maybe this can be accomplished using macros. Right now I'm looking to see if OpenOffice supports batch renaming of bookmarks in a docx file. Help is appreciated.

Best Answer

You can do this with a VBA macro in Word. The below macro, which is taken from here (and discussed in greater detail on the Web page), will prepend "NEW_" to each existing bookmark name, but it can be adapted to rename the bookmarks with whatever conventions are desired.

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
Related Question