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!

List Word Bookmarks with VBA

Status
Not open for further replies.

PeterJ01

Technical User
Jul 9, 2009
10
AU

Is there any code that will provide a list of all the names of the Bookmarks in Word?

regards,
Peter
 
Hi Peter,

This isn't especially difficult - it's basically a case of looping through the bookmarks collection and deciding what you want to do with each bookmark (eg retrieve its name and/or its contents) and adding the code to to output what you've retrieved where you want it to go (which you haven't told us). What have you coded so far?


Cheers
[MS MVP - Word]
 


Code:
    Dim bk As Bookmark
    For Each bk In Bookmarks
        MsgBox bk.Name
    Next


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Please note that the code Skip posted - or any other code that goes through the Bookmark collection - returns the bookmark names in alphabetical order, NOT the order they are in the document.

This may, or may not, be significant. If you have nested bookmarks, it may be significant.

Further, it will not return hidden bookmarks.

I will somewhat echo macropod. What exactly are you trying to do, and what have you done so far? Skip's code does not "provide a list", as it messages each name one at a time.

What do you mean - exactly - by "provide a list"? You could easily take Skip's code and instead of messaging, you could append each name into one string. This would be a "list".

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

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

Gerry
 
Thanks for these replies. I am proficient in Excel VBA, but never worked with Word VBA. Until the other day, I never heard of Word bookmarks :( In fact, I have alwaysw disliked using Word, as most of the time I get bogged down in formatting.

I was looking for a list of bookmarks so that I could see th exact names to use when I was coding VBA. Being in alphabetic order is a help. I intend transferring an Excel data array to Word, staring at the bookmark.

regards,
Peter

 
Hi Peter,

If you're transferring data to a bookmarked location, you need to know the correct bookmark's name in advance. Simply getting a list of bookmark names won't tell you which one to use. Still, if it's a list of bookmark names you want, you can modify Skip's code to:
Code:
Sub ListBookmarks()
Dim bk As Bookmark, bkList As String
For Each bk In ActiveDocument.Bookmarks
  bkList = bkList & bk.Name & vbCr
Next
MsgBox bkList
End Sub
Note that this doesn't tell you anything you can't already see by going to Insert|Bookmark or Insert|Cross-Reference > Bookmarks.


Cheers
[MS MVP - Word]
 
Have to agree with macropod. Just having a list may not be all that helpful.

"In fact, I have alwaysw disliked using Word, as most of the time I get bogged down in formatting."

The key to getting beyond that is to use Word the way it is meant to be used. With Styles. Once you get using styles down, 99% of all formatting issues are covered.

"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