(VBA) Return bookmark name or number from selected bookmark

Anonymous
2016-07-03T03:19:47+00:00

I wish to create a routine, running from a shortcut, which will interrogate the selected (or next if none selected) bookmark and depending on that bookmark name (or number) go to a predetermined bookmark, whereby the shortcut can repeat the action going to predetermined document places.  This may result in the selection point jumping all over the document, which is what I want.

There does not appear to be a wdGotoNext for bookmarks or a call which will return the selected bookmark name or number.

Has any one put together a routine which returns bookmark name or number, if "yes" any help much appreciated.

Ian B

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2016-07-03T04:42:53+00:00

    The following code will display the name of the first bookmark in the selected text and if there is none in the selected text, it will display the name of the first bookmark in the document after the start of the selected text if there is one.  Otherwise it will display a message advising that there are no bookmarks in the document after the start of the selected text:

    Dim rng As Range

    Set rng = Selection.Range

    If rng.Bookmarks.count > 0 Then

        MsgBox rng.Bookmarks(1).Name

    Else

        rng.End = ActiveDocument.Range.End

        If rng.Bookmarks.count > 0 Then

            MsgBox rng.Bookmarks(1).Name

        Else

            MsgBox "There are no bookmarks in the document after the start of the selected text."

        End If

    End If

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful