Printing Out Names of Bookmarks in Document

L

LA Lawyer

Is there a way, using VBA in Word 2007, to generate a list, in another Word
document, of all of the bookmarks in a document?
 
J

Jean-Guy Marcil

LA Lawyer was telling us:
LA Lawyer nous racontait que :
Is there a way, using VBA in Word 2007, to generate a list, in
another Word document, of all of the bookmarks in a document?

This will do the trick:

Sub Test()

Dim strBookMark As String
Dim bkmColl As Bookmarks
Dim i As Long
Dim docBook As Document

Set bkmColl = ActiveDocument.Bookmarks

With bkmColl
For i = 1 To .Count
strBookMark = strBookMark & .Item(i).Name & Chr(13)
Next
End With

Set docBook = Documents.Add

docBook.Range.Text = strBookMark

End Sub
 
J

Jay Freedman

Is there a way, using VBA in Word 2007, to generate a list, in another Word
document, of all of the bookmarks in a document?

Yes, it's quite simple:

Sub x()
Dim doc1 As Document, doc2 As Document
Dim bk As Bookmark

Set doc1 = ActiveDocument
If doc1.Bookmarks.Count = 0 Then
MsgBox "This document contains no bookmarks."
Exit Sub
End If

Set doc2 = Documents.Add

For Each bk In doc1.Bookmarks
doc2.Range.InsertAfter bk.Name & vbCr
Next
End Sub

If you want the list sorted in the order of the locations of the bookmarks
rather than alphabetically by name, change the For Each line to

For Each bk In doc1.Range.Bookmarks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top