Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - Display all bookmarks and names in new doc

Status
Not open for further replies.

MarcLodge

Programmer
Feb 26, 2002
1,886
GB
Hi all,
I have a number of documents that have numerous bookmarks in, named Text123 or Data456 etc. The bookmarks are not in order and are kind of all over the place. What I would like to be able to do is run a VBA macro that creates a new document and writes every bookmark name followed by the bookmark text to that new document.

I'm usingBookmarks(1).Range.Text in order to obtain the text and bookmarks(1).name to get the name, but I'm just struggling to get the name, as text, in the new document.

Any help greatly accepted, and my apologies if this is a very easy question - my VBA knowledge is very limited.

Marc
 
Hi Marc,

The following macro puts the details you're after into a tab-separated string that you could output to the new document:
Code:
Sub ListBkMrks()
Dim oBkMrk As Bookmark, StrBkMks As String
If ActiveDocument.Bookmarks.Count > 0 Then
  With Selection
    .EndKey Unit:=wdStory
    StrBkMks = "Bookmark" & vbTab & "Contents"
    For Each oBkMrk In ActiveDocument.Bookmarks
      StrBkMks = StrBkMks & vbCrLf & oBkMrk.Name & vbTab & oBkMrk.Range.Text
    Next oBkMrk
  End With
End If
End Sub


Cheers
[MS MVP - Word]
 
An alternative:
Code:
Sub ListBkMrks()
Dim oBkMrk As Bookmark
Dim StrBkMks As String
Dim ThisDoc As Document
Dim ListingDoc As Document

Set ThisDoc = ActiveDocument
Set ListingDoc = Documents.Add

If ThisDoc.Bookmarks.Count > 0 Then
    For Each oBkMrk In ThisDoc.Bookmarks
      StrBkMks = vbCrLf & "Bookmark " & oBkMrk.Name & _
         " content is: " & oBkMrk.Range.Text
      ListingDoc.Range.InsertAfter StrBkMks
    Next oBkMrk
End If
End Sub
which makes a new document listing the bookmarks and their contents.

Note however that the Bookmarks collection (used by For Each oBkMrk) will list them in alphabetical order, NOT - repeat NOT - the order they occur in the document.

In activedocument, bookmarks:

Client
Address
Something

will be listed as:

Bookmark Address content is: whatever, yadda
Bookmark Client content is: this is some text
Bookmark Something content is: something

in the new document.


"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top