Find a Selected Bookmark Name in Word VBA
Question details
Find the name of a bookmark associated with the current selection, or find a bookmark later in the document using Word VBA.

- Product
- Microsoft Word VBA
- Device & OS
- not provided
- Scenario
- A Word macro needs to identify a bookmark at the current selection or search beyond it.
- Observed behavior
- The desired result is to display a bookmark name when one is found and show a message when the search finds none.
Open the document in desktop Word and place the selection in the main document body. Make sure you can access the VBA editor and run macros under your organization's security settings.
Inspect the Selection Range's Bookmarks Collection
Use this macro to check the selection first, then extend the search range to the end of the main document.
The macro reports the first bookmark returned by the range's Bookmarks collection. If several bookmarks are returned, this is not necessarily a guarantee that it is the nearest bookmark by document position.
In Word, select Developer > Visual Basic. If the Developer tab is hidden, enable it through Word's ribbon customization settings.
In the VBA editor, select the document or an appropriate project, then choose Insert > Module.
Paste this code into the module: Sub FindSelectedBookmarkName() 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 after the selection." End If End If End Sub
Return to the document, select text or place the insertion point in the main document body, then run FindSelectedBookmarkName from Developer > Macros. Read the bookmark name or the no-bookmarks message.

Edit Word Documents with WPS Writer
WPS Writer is a free, lightweight alternative with a familiar interface for editing documents and moving existing files into a new workflow. The Word VBA macro in this article is specific to Microsoft Word; check macro support before migrating an automated workflow.

Frequently Asked Questions
How do I get the name of a bookmark in the current selection?
In Word VBA, set a Range to Selection.Range and check rng.Bookmarks.Count. If the count is greater than zero, rng.Bookmarks(1).Name returns the first name in that collection.
How can I search beyond the current selection?
For a selection in the main document body, set rng.End to ActiveDocument.Range.End, then check rng.Bookmarks.Count again. This extends the range from its original start through the end of the document.
Does Bookmarks(1) always identify the nearest following bookmark?
No. It returns the first item in the range's Bookmarks collection. If you need the nearest bookmark by position, compare bookmark range positions explicitly.
Where can I run the macro in Word?
Paste it into a module through Developer > Visual Basic > Insert > Module. Then use Developer > Macros to select and run FindSelectedBookmarkName, subject to your macro security settings.




