Word – Renaming a bookmark in Word 2010

microsoft wordmicrosoft-word-2010

I often use bookmarks in Word in order to create technical documents with internal links to citations and commonly used information that needs to be consistent throughout the document. You can create them by selecting the text and then going to the menu Insert -> Links -> Bookmark and assigning a name, then going to Insert -> Links -> Citations and selecting the bookmark to insert. This will then insert the text of that bookmark elsewhere in the document.

As an example of why I do this: I often have a front page listing the document state and that information is then repeated in the footer, using a bookmark I can set up a link to that text and then simply insert that link in the footer and it will update automatically whenever I go to the print preview or update all fields in the document. This way I can avoid doing a search and replace and just know that the correct text will be updated in the correct places.

enter image description here

Now lets say I have a bookmark that I've just roughly named, just to get it into place:
enter image description here

There is no right-click to rename a bookmark and I cannot find any document property that will let me amend it.

I am able to insert a NEW bookmark by selecting the same text and then deleting the old one but then you get this:
enter image description here

You then have to right click the field and point it back at the (now effectively renamed) bookmark.

This isn't completely usable, especially in a large document where this bookmark is used multiple times.

I have Googled around and seen several VBA scripts on the web such as in this question but as they effectively create a new boomark and delete the old one they create the same problem as above and are less than ideal.

Is there a simpler or more effective way to rename bookmarks in Word documents? Or am I possibly using the Wrong Tool™ for this job?

Best Answer

Is there a simpler or more effective way to rename bookmarks in Word documents?

There is no rename function built-in to Word. There are a couple of options to work around this lack:

  1. Use an Add-in.

  2. Use VBA.


Add-in Solution

Use the Bookmark Tool Add-In

It offers a friendly user interface for doing everything the standard Bookmark Dialog box will do plus much more.

Bookmark Tool was developed for Word 2003. It is wholly functional with Word 2007/2010.

...

With the "Add/Rename Bookmark" section, adding bookmarks is a breeze.

  • As in the standard dialog, you simply select text, type a name in the field, and click "Add."
  • Unlike the standard dialog, Bookmark Tool restricts key entry to valid bookmark name characters and alerts you if you attempt to create a duplicate bookmark name.
  • You can also use this section to rename an existing bookmark.

    enter image description here

Source Bookmark Tool Add-In


VBA solution

There isn't a rename function. You have to delete the old name and mark the range with a new bookmark name. Here's sample VBA code:

Sub ReNameBookMark()
    Dim doc As Word.Document
    Dim rng As Word.Range
    Dim bmk As Word.Bookmark
    Dim inpBookmark, repBookmark, fieldStr As String

    Set doc = Word.ActiveDocument

    inpBookmark = InputBox("Enter bookmark name that you want to be replaced:", "BookMark Replace")
    repBookmark = InputBox("Enter bookmark name replace with:", "BookMark Replace")

    Set rng = doc.Bookmarks(inpBookmark).Range
    Set bmk = doc.Bookmarks(inpBookmark)
    bmk.Delete
    rng.Bookmarks.Add (repBookmark)

    If doc.Fields.Count >= 1 Then
        For i = 1 To doc.Fields.Count
            fieldStr = doc.Fields(i).Code.Text
            If Left(fieldStr, 4) = " REF" Then
                doc.Fields(i).Code.Text = Replace(fieldStr, inpBookmark, repBookmark, , 1, vbTextCompare)
                doc.Fields(i).Update
            End If

            'MsgBox "Code = " & doc.Fields(i).Code & vbCr & "Result = " & doc.Fields(i).Result & vbCr
        Next i
    End If
End Sub

Source Change the "name" of a bookmark not the text of it, with an additional loop to run through the fields in the document to change any that might be referencing the bookmark being renamed.

Care should be taken using this script. For example renaming any bookmarks that are simply named "REF" (or an upper or lower case variant of such) will break ALL references in amusing and unexpected ways. This is meant as an example and rough fix only.

If you want to batch rename multiple bookmarks in one go see Is there a simpler or more effective way to rename bookmarks in Word documents? which also includes sample VBA code.

Related Question