Document Editing Problems

Find a Selected Bookmark Name in Word VBA

Steven ParkerSteven Parker Nov 25, 2026 868 views

Question details

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

Find a Selected Bookmark Name in 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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Word, select Developer > Visual Basic. If the Developer tab is hidden, enable it through Word's ribbon customization settings.

2
Insert a module

In the VBA editor, select the document or an appropriate project, then choose Insert > Module.

3
Add the macro

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

4
Run the macro

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.

Inspect the Selection Range's Bookmarks Collection
Scope of the search: This example searches from a selection in the main document body to the end of that document. Selections in headers, footnotes, or other document stories require a range in the corresponding story.
Free Microsoft Office alternative

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.

Edit and navigate long documents in WPS WriterCompatible with Microsoft Word DOC and DOCX formatsFamiliar document-editing interfaceFree, lightweight option for everyday office work
QA img-9

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.