bottom
Great WordTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > WordTips Home > Editing > Bookmarks > Getting the Names of Defined Bookmarks

Getting the Names of Defined Bookmarks

Summary: Want to find out the names of bookmarks? This VBA code will do it for you. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)

Chances are good that you already know how to use bookmarks. (Other WordTips have described how to define and manipulate them.) If you are creating macros, you may have a need to retrieve the names of the bookmarks defined within a document.

VBA lets you retrieve bookmark names by using the Name property with members of the Bookmarks collection. The syntax of the statement is as follows:

x = ActiveDocument.Bookmarks(y).Name

where y is the offset pointer to the bookmark name wanted. After executing the statement, x will contain the name of the bookmark specified by the pointer. To make this function useful, you really should read all the bookmark names into an array, which you can then manipulate. The following code fragment will do the trick:

Dim Bmk() As String
Dim x As Integer, J As Integer

x = ActiveDocument.Bookmarks.Count
ReDim Bmk(x)
For j = 1 to x
    Bmk(j) = ActiveDocument.Bookmarks(j).Name
Next j

Notice that this code fragment uses the ReDim statement, which allows you to dynamically change the number of elements in an array. The reason for this approach is quite simple: The Bmk array needs to have as many elements as there are defined bookmarks. However, you can't know before executing the third line of this code exactly how many that is. VBA requires that all dimensioning (using Dim) be done before any actual program code is executed. Thus, the first Dim for Bmk is to satisfy VBA, and the ReDim of Bmk is done to set the array to the necessary size.

Tip #796 applies to Microsoft Word versions: 97 | 2000 | 2002 | 2003


Find and Replace Almost Anything! An invaluable resource for learning how to harness the full power of Word's search and replace capabilities. You'll discover everything you need in order to master all the intricacies of finding and replacing elements of your document, including the super-powerful "wildcard searches" available in Word.
 
Check out WordTips: Find and Replace today!

Helpful Links

Ask a Word Question
Make a Comment

Tips.Net Home
Tips.Net Store

WordTips FAQ
WordTips Premium

Learn Access Now

Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips

Advertise on the
WordTips Site

 

Great Info!

Get tips like this every week in WordTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)