Bookmark name from PreviousBookmarkID

J

jw

Word 2003 sp2

I want to return the name of the bookmark preceding the cursor. By
analogy with the second example in the VBA Help file entitled
"PreviousBookmarkID property", I wrote the following preliminary
code:

Dim xBookmarkNumber As Long
xBookmarkNumber = Selection.PreviousBookmarkID
MsgBox xBookmarkNumber
MsgBox ActiveDocument.Bookmarks(xBookmarkNumber).Name

When I tested this code by placing the cursor after the first bookmark
in the document, xBookmarkNumber was identified correctly as 1.
However, the name returned by
ActiveDocument.Bookmarks(xBookmarkNumber).Name was that of a different
bookmark. I finally figured out that the name being returned was the
name of the first bookmark IF THE BOOKMARKS ARE SORTED BY NAME!!!

Can anybody tell me how to retrieve the name of the previous bookmark?
Thanks.

PS: I realize that the following code will retrieve the name:

MsgBox Selection.Bookmarks(1).Name

However, this requires that you select text either surrounding a
placeholder bookmark or within an enclosure bookmark, which I don't
want to have to take the trouble to do. (And in any case, the code
provided in the Help file doesn't work properly!)
 
J

Jay Freedman

Odd as it may seem, the syntax

ActiveDocument.Bookmarks(xBookmarkNumber)

returns the bookmarks in alphabetic order by name, while

ActiveDocument.Range.Bookmarks(xBookmarkNumber)

returns them in location order.

In the Insert > Bookmark dialog, the option buttons for sorting the
list by name or by location work exactly the same way.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
J

jw

Excellent. Thanks very much.


Jay said:
Odd as it may seem, the syntax

ActiveDocument.Bookmarks(xBookmarkNumber)

returns the bookmarks in alphabetic order by name, while

ActiveDocument.Range.Bookmarks(xBookmarkNumber)

returns them in location order.

In the Insert > Bookmark dialog, the option buttons for sorting the
list by name or by location work exactly the same way.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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