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.
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.