BookmarkID wrong

S

Smallweed

When I run the following I get a "requested member of the collection does not
exist" error:

If Selection.BookmarkID <> 0 Then
MsgBox ActiveDocument.Bookmarks(Selection.BookmarkID).Name
End If

It's because the bookmark enclosing my cursor happens to be 13 and yet the
ActiveDocument.Bookmarks.Count is only 11.

How can this be??
 
J

Jean-Guy Marcil

Smallweed was telling us:
Smallweed nous racontait que :
When I run the following I get a "requested member of the collection
does not exist" error:

If Selection.BookmarkID <> 0 Then
MsgBox ActiveDocument.Bookmarks(Selection.BookmarkID).Name
End If

It's because the bookmark enclosing my cursor happens to be 13 and
yet the ActiveDocument.Bookmarks.Count is only 11.

How can this be??

You have hidden bookmarks in your document,
The total count is higher than the visible count.

Check the "Hidden Bookmark" option in the bookmark dialog box to see the
hidden ones, you may have to close and re-open the dialog for the setting to
take effects.

You would need:

If Selection.BookmarkID <> 0 Then
ActiveDocument.Bookmarks.ShowHidden = True
MsgBox ActiveDocument.Bookmarks(Selection.BookmarkID).Name
ActiveDocument.Bookmarks.ShowHidden = False
End If


This might not be reliable because this will give you the ID number of the
current bookmark according to location, however, Bookmarks(Index) is based
on the Bookmark collection which is indexed according to creation order.

So, if all you ant is the current bookmark name, use:

If Selection.Bookmarks.Count > 0 Then
MsgBox Selection.Range.Bookmarks(1).Name
End If


You nay want to add more code in order to check if the current selection
includes more than one bookmark...
 

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