Working With Bookmarks

M

MT DOJ Help Desk

Word 2000

I thought this was going to be snap, but I've run into a problem and I'm not
sure what I'm doing wrong. I have a document that uses a lot of bookmarks.
I have macros that use the bookmarks to select blocks of text. Not every
bookmark in the document represents a block of text that I need to process,
and bookmarks can be deleted while the data in the document is being
processed. I felt that I couldn't just use the bookmarks collection because
that collection contains more bookmarks than I need to worry about, and the
contents of the collection would be changing on the fly. I decided that I
needed a static list of bookmark names that I could reference, so I wrote a
macro that builds an array which contains only the names of the bookmarks
that I need to work with. My thinking was that I could index that array to
pull out the bookmark names, and then jump to those bookmarks by name.
However, when I try this, I get:

Run-time error '9':
Subscript out of range

Here's the code:

Option Base 1

Dim BookmarkName As String

Sub NextLocate()
Application.ScreenUpdating = False
NextLocateCounter = NextLocateCounter + 1
BookmarkName = BookmarkCollection(NextLocateCounter) <<< This is
where the error occurs
Selection.GoTo what:=wdGoToBookmark, Name:=BookmarkName
Application.ScreenUpdating = True
<Other stuff>
End Sub

I'm not using a loop because I don't want this routine to run through the
entire array at once. Instead, each time a toolbar button is clicked the
macro is supposed to grab the next bookmark name from the array (not
necessarily the next bookmark in the document) and goto that bookmark. How
can I pull the bookmark names out of the array and use those names to
navigate to bookmarks?

On a related note, but something that is more for the sake of curiosity, is
this question: Is there a way to get the current cursor location, and
return the name of the bookmark that contains that location?

-- Tom

MT DOJ Help Desk

Making the world a safer place.
 
J

Jezebel

Your function seems a tad odd. If you call it repeatedly, sooner or later
NextLocateCounter must exceed the upper bound of the array.

If nothing else you should check:

If NextLocateCounter > ubound(BookmarkCollection) then ....

Hard to comment further without knowing anything about how you dimension the
collection array and how you initialize the counter.



You can get the first bookmark (if any) containing the selection using

Selection.Bookmarks(1)

Bear in mind there might (at least in theory) be more than one bookmark
containing the selection.
 
M

MT DOJ Help Desk

Thanks for the reply. You're right about the counter exceeding the upper
bound of the array. I wasn't able to get it to work the first time, so I
never got that far, but I would have eventually run into that error and
fixed it.

I think that I've figured out a way around my problem. The bookmarks in the
document are created by a macro as blocks of text are pasted into the
document. That macro creates the bookmarks according to a set naming
convention. So I really don't need the array at all. I can just use the
bookmarks collection, and make my routine filter out any bookmarks that
don't conform to that naming convention. That will insure that only those
bookmarks that I need to work with will be processed by the routine.
 

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