A client wanted a VBA routine which would enable the user of the document to move to the next bookmrk in the same way that pressing F5 would do. However, there was a slight twist to the requirement; this was that this would only work for bookmarks beginning with a certain string. What the client wanted was for this to move between all bookmarks beginning with "temp".
Below is the code:
Option Explicit Const BOOKMARK_LABEL As String = "temp" ' Sub FindNextBookmark() Dim oBookmark As Bookmark Dim nCurrentPosition As Long Dim nPosition As Long Dim nFirstPosition As Long Dim sFirstBookmark As String Dim nNextPosition As Long Dim sNextBookmark As String If Documents.Count > 0 Then nCurrentPosition = Selection.Range.Start nNextPosition = 0 nFirstPosition = 0 For Each oBookmark In ActiveDocument.Bookmarks If InStr(1, oBookmark.Name, BOOKMARK_LABEL, vbTextCompare) = 1 Then nPosition = oBookmark.Start ' Check for the first bookmark on the document in case ' the selection wraps around If nFirstPosition = 0 Or nPosition < nFirstPosition Then nFirstPosition = nPosition sFirstBookmark = oBookmark.Name End If ' Check for the next position ahead of the current position If nPosition > nCurrentPosition And (nPosition < nNextPosition Or nNextPosition = 0) Then nNextPosition = nPosition sNextBookmark = oBookmark.Name End If End If Next oBookmark If nNextPosition > 0 Then ActiveDocument.Bookmarks(sNextBookmark).Range.Select ElseIf nFirstPosition > 0 Then ActiveDocument.Bookmarks(sFirstBookmark).Range.Select End If End If End Sub
As with most software projects the hard part is working out what the problem is and how to solve it. After a few moment's consideration I came up with this following solution. No doubt there are other solutions possible, some more elegant than this one. However, this is rather simple and straightforward.
The main component of this routine is a loop which goes through each bookmark in the document's collection (after making sure that there is at least one document open, of course) and then checking to see if the bookmark begins with the string of interest, in this case "temp".
If the user wanted to this to work without having the string and just to rattle through each bookmark in turn then the If Instr() Then...End If statements can be removed.
All that happens is then for each of these bookmarks the position of the bookmark (measured in characters) is recorded. If it is the first bookmark of the document or whether it's the next bookmark to the current position then the details are recorded.
After each bookmark is analysed then if there is a bookmark of interest after the current position the the next one is selected. Failing that, the first bookmark of interest in the document, if it exists, is selected so that the user wraps around the bookmarks in positional order. If there are no bookmarks of interest located then the user's selection is not altered.
So despite the rather complicated specification a rather simple piece of code was all that was required.
This code can be downloaded from here.
If there are any suggestions for updates or comments then please drop us a mail at email@example.com.