Others

How to Store Complete Bookmark Text in a Variable Using Word VBA

Huda QurayshiHuda Qurayshi Nov 2, 2026 869 views

Question details

A Word VBA macro fails to capture a complete date string from a bookmark, extracting only the first character or word because of hyphens. The full text is required to set a file name and save path.

How to Store Complete Bookmark Text in a Variable Using Word VBA
Product
Word
Device & OS
not provided
Scenario
Automating document saving by extracting a date like '2021-11-10' from a document bookmark using VBA.
Observed behavior
The macro captures only the year or the first character instead of the entire date string because Word treats hyphens as word delimiters, or because the text is positioned at the bookmark rather than inside it.
Before you start

Ensure you have the Developer tab enabled in your ribbon and that your document is saved as a Macro-Enabled Document (.docm). Turn on bookmark visibility in Options > Advanced to check if your date is actually enclosed within the bookmark brackets.

Solution 1Recommended

Extract Complete Bookmark Text Using Range and MoveEndUntil

Avoid using the Words collection, which truncates at hyphens. Instead, read the bookmark's Range and expand it dynamically if the text was inserted outside the bookmark boundaries.

Word treats punctuation like hyphens as word delimiters. Consequently, calling Range.Words(1) on a date like 2021-11-10 will stop before the first hyphen. By defining a Range object and verifying its length, you can instruct the macro to expand the selection until a space or carriage return is detected, successfully capturing the full string.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications (VBA) editor, and locate the module containing your macro.

2
Declare Variables

Declare your variables by adding 'Dim sMyString As String' and 'Dim rg As Range' at the beginning of your subroutine.

3
Set the Bookmark Range

Target your specific bookmark by adding the line: 'Set rg = ActiveDocument.Bookmarks("MeetingDate").Range'.

4
Expand the Range if Necessary

Add a conditional block to expand the range if the text was inserted immediately after the bookmark: 'If Len(rg.Text) = 0 Then rg.MoveEndUntil Cset:=" ,." & vbCr End If'.

5
Assign and Test Variable

Assign the captured range to your variable with 'sMyString = rg.Text' and use 'Debug.Print sMyString' to verify it captured the full date.

Extract Complete Bookmark Text Using Range and MoveEndUntil
Punctuation Considerations: Depending on the text immediately surrounding your date in the document, you may need to adjust the Cset string in the MoveEndUntil method to include other punctuation marks so the range stops expanding at the correct character.
Powerful VBA Support

Automate Documents with WPS Writer VBA

WPS Office offers comprehensive VBA macro support, allowing you to run your existing Word scripts, manipulate bookmarks programmatically, and automate complex document workflows with ease.

  1. 1. Enable Developer Tab: Open WPS Writer, go to the Options menu, and customize the ribbon to ensure the Developer tab is visible.
  2. 2. Launch the VBA Editor: Click the Developer tab and select 'Visual Basic', or press ALT + F11 to open the built-in VBA environment.
  3. 3. Insert Your Code: Paste your macro code into a Module. Your standard Word VBA syntax for extracting bookmark ranges will function natively.
  4. 4. Execute Automation: Run your script to capture the bookmark text seamlessly and automate your document saving process.
Fully compatible with Microsoft Word .docm and .dotm macro filesIncludes a built-in Visual Basic Editor for seamless macro creationAccurately reads and manipulates ranges, bookmarks, and propertiesFree, lightweight, and highly compatible Office alternative
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA macro only return the year portion of a date string?

When using the Words(1) property in VBA, the object model treats punctuation marks like hyphens as delimiters. Because a date formatted as 2021-11-10 contains hyphens, it is treated as multiple distinct words, so Words(1) stops at '2021'. Accessing the Range.Text property bypasses this limitation.

How do I visually check if text is inside a bookmark?

You can reveal bookmarks by navigating to File > Options > Advanced. Under the 'Show document content' section, check 'Show bookmarks'. Text that is properly enclosed will appear within gray brackets, while empty or point-bookmarks will display as a large 'I' cursor mark.

Can I use the extracted bookmark text to automatically save the document?

Yes. Once you have stored the complete date text in a string variable, you can concatenate it with your base folder path and desired file extension. You then pass this complete path string to the ActiveDocument.SaveAs2 method to fully automate naming and saving your file.