Bookmarks Listed by Location

M

MT DOJ Help Desk

I have the following code (It's a test routine, so the code may be a bit
rough).

Sub TestBookmarkName()

NumBookmarks = Selection.Bookmarks.Count
For Counter = 1 To NumBookmarks
BookmarkName = ActiveDocument.Range.Bookmarks(Counter)
If Left(BookmarkName, 6) = "Record" Then
BookmarkNumber =
ActiveDocument.Bookmarks(BookmarkName).Range.BookmarkID + 1
BookmarkName = ActiveDocument.Bookmarks(BookmarkNumber)
Selection.GoTo what:=wdGoToBookmark, Name:=BookmarkName
End If
Next

End Sub

This works well, but with one problem--it gets the next bookmark as listed
by bookmark name. What I need is for it to get the next bookmark as listed
by location. Give that the whole point is to find the next bookmark by
location, this is a problem. :) I've figured out a couple of ways that I
can work around this issue, but none are as simple as this code. It would
be ideal if I could find a command that will order the bookmark collection
by location. Is there a way to do that, or am I stuck with a more round
about solution?
 
J

Jezebel

I've not tested this on the Selection, but with the document as a whole,
Doc.Bookmarks returns them in alphabetical order but Doc.RANGE.Bookmarks
returns them in location order. So by analogy, if you modify your code to
use

Selection.Range.Bookmarks

it might work as you need.
 
M

MT DOJ Help Desk

Thanks for the reply. I had played around with
ActiveDocument.Bookmarks.DefaultSorting = wdSortByLocation, but that only
changes the sorting in the Bookmarks dialog box. However, today I was able
to get it working by modifying the code slightly. Basically, the first time
BookmarkName gets set, I needed to be working with the selection and not the
ActiveDocument.Range, and the second time BookmarkName gets set, I needed to
be working with ActiveDocument.Range and also using .Name. Then I just
added an IF statement to handle situations when there is no text selected in
the document. Now it works perfectly.


Sub TestBookmarkName()

IndexJump = 3
If Selection.Range.Text = "" Then
IndexJump = 1
End If
NumBookmarks = Selection.Bookmarks.Count
For Counter = 1 To NumBookmarks
BookmarkName = Selection.Range.Bookmarks(Counter)
If Left(BookmarkName, 6) = "Record" Then
BookmarkNumber =
ActiveDocument.Bookmarks(BookmarkName).Range.BookmarkID + IndexJump
BookmarkName =
ActiveDocument.Range.Bookmarks(BookmarkNumber).Name
Selection.GoTo what:=wdGoToBookmark, Name:=BookmarkName
End If
Next

End Sub


-- Tom

MT DOJ Help Desk

Making the world a safer place.
 

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