How to Store Complete Bookmark Text in a Variable Using Word VBA
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.

- 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.
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.
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.
Press ALT + F11 to open the Visual Basic for Applications (VBA) editor, and locate the module containing your macro.
Declare your variables by adding 'Dim sMyString As String' and 'Dim rg As Range' at the beginning of your subroutine.
Target your specific bookmark by adding the line: 'Set rg = ActiveDocument.Bookmarks("MeetingDate").Range'.
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'.
Assign the captured range to your variable with 'sMyString = rg.Text' and use 'Debug.Print sMyString' to verify it captured the full date.

Ensure Text is Inside the Bookmark Enclosure
Bookmarks can either enclose text entirely or act as a single insertion point. Ensuring the date text is placed fully inside the bookmark allows for clean, straightforward extraction without expanding ranges.
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. Enable Developer Tab: Open WPS Writer, go to the Options menu, and customize the ribbon to ensure the Developer tab is visible.
- 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. Insert Your Code: Paste your macro code into a Module. Your standard Word VBA syntax for extracting bookmark ranges will function natively.
- 4. Execute Automation: Run your script to capture the bookmark text seamlessly and automate your document saving process.

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.




