Find a Word Table by Bookmark Name Using VBA
Question details
Find a table in a Word document using its bookmark name in VBA.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A VBA macro needs to locate and process a table associated with a known bookmark.
- Observed behavior
- The table range does not reliably provide the bookmark name through tbl.Range.Bookmark.Text.
In Word, open the document and use Insert > Bookmark to check the exact bookmark name. Use desktop Word with VBA available, and run macros only from trusted sources.
Find the bookmark and check its range for a table
Use the document's Bookmarks collection to locate the named bookmark, then find the first table associated with its range.
Bookmarks are document-level objects. Look up the bookmark by name rather than trying to read its name from the table range.
The macro first checks the bookmark range's Tables collection. If that collection is empty, it checks for a table whose range overlaps the bookmark or contains its insertion point.
In desktop Word, open the document, then choose Developer > Visual Basic. If Developer is hidden in Windows, enable it under File > Options > Customize Ribbon.
In the VBA editor, choose Insert > Module and paste this code: Sub FindBookmarkedTable() Dim doc As Document Dim bmRange As Range Dim tbl As Table Dim candidate As Table Dim bookmarkName As String Set doc = ActiveDocument bookmarkName = "TableBookmark" If Not doc.Bookmarks.Exists(bookmarkName) Then MsgBox "Bookmark not found: " & bookmarkName Exit Sub End If Set bmRange = doc.Bookmarks(bookmarkName).Range If bmRange.Tables.Count > 0 Then Set tbl = bmRange.Tables(1) Else For Each candidate In doc.Tables If bmRange.Start = bmRange.End Then If bmRange.Start >= candidate.Range.Start And _ bmRange.Start < candidate.Range.End Then Set tbl = candidate Exit For End If Else If bmRange.Start < candidate.Range.End And _ bmRange.End > candidate.Range.Start Then Set tbl = candidate Exit For End If End If Next candidate End If If tbl Is Nothing Then MsgBox "No table is associated with the bookmark." Else tbl.Select End If End Sub
Replace TableBookmark in the bookmarkName assignment with the name shown in Insert > Bookmark.
Return to Word and choose Developer > Macros. Select FindBookmarkedTable and click Run; the macro selects the first matching table or displays a message if it cannot find one.

Edit Word documents and tables with WPS Writer
For everyday document editing, WPS Office is a free, lightweight alternative with a familiar interface and Microsoft Office format compatibility for an easier transition. It does not fix Word's VBA behavior.

Frequently Asked Questions
Why shouldn't I use tbl.Range.Bookmark.Text to get the bookmark name?
A bookmark is a document-level object, and its name is not reliably exposed through that table-range expression. Use doc.Bookmarks(name) to retrieve the bookmark and inspect its Range.
How do I check whether a bookmark exists before using it?
In VBA, call doc.Bookmarks.Exists("TableBookmark") before accessing doc.Bookmarks("TableBookmark"). Replace the example name with your bookmark name.
What if the bookmark is only an insertion point inside a table?
A collapsed bookmark has the same Start and End positions. If its Range.Tables collection is empty, compare that position with each table's Range.Start and Range.End, as shown in the macro.
How can I keep the macro in the document?
Choose File > Save As and save the document as a Word Macro-Enabled Document (.docm). Run the macro only if you trust its source.




