Have you ever tried to print a list of bookmarks in a Word document? Here, I’ll show you methods to achieve it.
Extract All Bookmarks and Print Using VBA
The VBA code below will help you list all bookmarks from the current document to a new document, and you can manually print the extracted bookmarks as needed. Follow these steps:
Open the document you want to print bookmarks from, press Alt + F11 to open the Microsoft Visual Basic for Applications window.
In the Microsoft Visual Basic for Applications window, click Insert > Module, then copy and paste the code below into the Module window:
Sub ExtractBookmarksInADoc()
‘Updated by Extendoffice 20181123
Dim xRow As Long
Dim xTable As Table
Dim xDoc As Document
Dim xBookMark As Bookmark
Dim xBookMarkDoc As Document
Dim xParagraph As Paragraph
Set xDoc = ActiveDocument
If xDoc.Bookmarks.Count = 0 Then
MsgBox “There is no bookmark in this document”, vbInformation, “KuTools for Word”
Exit Sub
End If
Set xBookMarkDoc = Documents.Add
xRow = 1
Selection.TypeText “Bookmarks in ” & “‘” & xDoc.Name & “‘”
Set xTable = Selection.Tables.Add(Selection.Range, 1, 3)
xTable.Borders.Enable = True
With xTable
.Cell(xRow, 1).Range.Text = “Name”
.Cell(xRow, 2).Range.Text = “Texts”
.Cell(xRow, 3).Range.Text = “Page Number”
For Each xBookMark In xDoc.Bookmarks
xTable.Rows.Add
xRow = xRow + 1
.Cell(xRow, 1).Range.Text = xBookMark.Name
.Cell(xRow, 2).Range.Text = xBookMark.Range.Text
.Cell(xRow, 3).Range.Text = xBookMark.Range.Information(wdActiveEndAdjustedPageNumber)
xDoc.Hyperlinks.Add Anchor:=.Cell(xRow, 3).Range, Address:=xDoc.Name, _
SubAddress:=xBookMark.Name, TextToDisplay:=.Cell(xRow, 3).Range.Text
Next
End With
xBookMarkDoc.SaveAs xDoc.Path & “\” & “Bookmarks in ” & xDoc.Name
End Sub
Press F5 to run the code.
A new document will be automatically created with all bookmarks from the specified document inside.
You can click File > Print to print the list of extracted bookmarks when needed.
Print All Bookmarks Directly Using VBA
If you want to print all bookmarks directly in the current document, follow these steps:
Open the document you want to print bookmarks from, press Alt + F11 to open the Microsoft Visual Basic for Applications window.
In the Microsoft Visual Basic for Applications window, click Insert > Module, then copy and paste the code below into the Module window:
Sub ExtractBookmarksInADoc()
‘Updated by Extendoffice 20181123
Dim xRow As Long
Dim xTable As Table
Dim xDoc As Document
Dim xBookMark As Bookmark
Dim xBookMarkDoc As Document
Dim xParagraph As Paragraph
On Error Resume Next
Set xDoc = ActiveDocument
If xDoc.Bookmarks.Count = 0 Then
MsgBox “There is no bookmark in this document”, vbInformation, “KuTools for Word”
Exit Sub
End If
Set xBookMarkDoc = Documents.Add
xRow = 1
Selection.TypeText “Bookmarks in ” & “‘” & xDoc.Name & “‘”
Set xTable = Selection.Tables.Add(Selection.Range, 1, 3)
xTable.Borders.Enable = True
With xTable
.Cell(xRow, 1).Range.Text = “Name”
.Cell(xRow, 2).Range.Text = “Texts”
.Cell(xRow, 3).Range.Text = “Page Number”
For Each xBookMark In xDoc.Bookmarks
xTable.Rows.Add
xRow = xRow + 1
.Cell(xRow, 1).Range.Text = xBookMark.Name
.Cell(xRow, 2).Range.Text = xBookMark.Range.Text
.Cell(xRow, 3).Range.Text = xBookMark.Range.Information(wdActiveEndAdjustedPageNumber)
xDoc.Hyperlinks.Add Anchor:=.Cell(xRow, 3).Range, Address:=xDoc.Name, _
SubAddress:=xBookMark.Name, TextToDisplay:=.Cell(xRow, 3).Range.Text
Next
End With
xBookMarkDoc.SaveAs xDoc.Path & “\” & “Bookmarks in ” & xDoc.Name
xBookMarkDoc.PrintOut
xBookMarkDoc.Close
Kill xBookMarkDoc.Path
End Sub
Press F5 to print all bookmarks directly.