How to Batch Change Multiple Bookmark Names in Your Word Document

In today’s article, we are going to provide you with the way to batch change multiple bookmark names in your Word document.

There is a little tricky to change a bookmark name in Word document. And in one of our previous article, we have explained ways to you. For detailed information, you can refer to this link: 2 Correct Ways to Change a Bookmark Name in Your Word Document

This time, we want to offer you the way to batch change multiple bookmark names in your document.Batch Change Multiple Bookmark Names in Your Word Document

Batch Change Multiple Bookmark Names via VBA

  1. First and foremost, insert a 2-column table of multiple rows at the end of your target document. In the table, enter the original bookmark names in the first column. And enter new names in the second. Also make sure you put cursor inside the table. Here is the example:Insert a Table
  2. Then invoke the VBA editor in Word by pressing “Alt+ F11”.
  3. Next in the editor, click “Normal” on the left column.
  4. And on the menu bar above, click “Insert” tab.
  5. On the drop-down menu of “Insert”, choose “Module”.Click "Normal"->Click "Insert"->Click "Module"
  6. Double click on new module to open it and then paste following codes in the coding area:
Sub BatchChangeTheBookMarkNameAndUpdateCrossReference()
  Dim nCurrentTableIndex As Integer
  Dim objTable As Table
  Dim nRowNumber As Integer
  Dim objOriBookMarkList As Cell
  Dim objOriBookMarkListR As Range
  Dim objNewBookMarkListR As Range
  Dim strBookMarkName As String
  Dim strNewName As String
  Dim objBookMarkRange As Range
  Dim objField As Field
  Dim strFieldCode As String
 
  nCurrentTableIndex = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count
  Set objTable = ActiveDocument.Tables(nCurrentTableIndex)
  nRowNumber = 1
 
  For Each objOriBookMarkList In objTable.Columns(1).Cells
    Set objOriBookMarkListR = objOriBookMarkList.Range
    objOriBookMarkListR.MoveEnd Unit:=wdCharacter, Count:=-1
    Set objNewBookMarkListR = objTable.Cell(nRowNumber, 2).Range
    objNewBookMarkListR.MoveEnd Unit:=wdCharacter, Count:=-1
 
    If objOriBookMarkListR.Text <> "" Then
      strBookMarkName = objOriBookMarkListR.Text
      strNewName = objNewBookMarkListR.Text
    End If
 
    With ActiveDocument
      If .Bookmarks.Exists(strBookMarkName) Then
        Set objBookMarkRange = .Bookmarks(strBookMarkName).Range
        .Bookmarks(strBookMarkName).Delete
        .Bookmarks.Add Name:=strNewName, Range:=objBookMarkRange
 
        ' Update the cross-reference
        If .Fields.Count >= 1 Then
          For Each objField In .Fields
            strFieldCode = objField.Code.Text
            If strFieldCode = " REF " & strBookMarkName & " \h " Then
              objField.Code.Text = Replace(strFieldCode, strBookMarkName, strNewName, , 1, vbTextCompare)
              objField.Update
            End If
          Next objField
        End If
      Else
        MsgBox ("The bookmark: " & strBookMarkName & " is not founded.")
      End If
    End With

    Set objBookMarkRange = Nothing
    nRowNumber = nRowNumber + 1
  Next
  MsgBox ("All bookmarks in the table list have been renamed.")
End Sub
  1. Last but not the least, click “Run” or hit “F5” to execute codes.Paste Macro->Click "Run"

When the process is done, there will be a message box, such as below:Result Box

You can also click “Insert” tab and then the “Bookmark” to trigger the “Bookmark” dialog box. Now you can see the bookmark names have been changed.Click "Insert"->Click "Bookmark"

Deal with Word Errors

Since Word is not immune to mistakes, it’s necessary to take the right action when Word corruption happens. Most of the time, you can retrieve data back by picking a proficient repairing tool.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including fix Excel and pdf repair software products. For more information visit www.datanumen.com

Leave a Reply

Your email address will not be published. Required fields are marked *