How to View and List Word Document Variables Using VBA
Question details
The user needs to inspect the hidden document variables stored in a Word file and requires a method to list each variable's name and value.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Extracting or viewing hidden document variables for inspection, troubleshooting, or later updates from external applications like Microsoft Access.
- Observed behavior
- Word lacks a built-in user interface or standard viewer to display document variables directly, requiring a programmatic or structural workaround.
Ensure your document is saved in a macro-enabled format (.docm) if you plan to save the VBA code, and confirm that the Developer tab is enabled in your ribbon settings to access the VBA editor.
Use VBA Code to Enumerate Document Variables
Write and execute a simple VBA macro to loop through all hidden document variables and display their names and values.
Since Word does not provide a built-in graphical interface for document variables, using Visual Basic for Applications (VBA) is the most efficient method to extract and view this hidden data.
Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications window.
Click on 'Insert' in the top menu and select 'Module' to create a new blank script window.
Paste the following code into the module window: Sub ListDocVariables() Dim oVariable As Variable For Each oVariable In ActiveDocument.Variables MsgBox oVariable.Name MsgBox oVariable.Value Next oVariable End Sub
Press F5 or click the 'Run' button. The macro will trigger a series of message boxes displaying the name and value of each variable stored in the document.

Inspect the XML File Structure (No Macros Required)
If you cannot or prefer not to run macros, you can view document variables by exploring the underlying XML structure of the Word file.
Use WPS Office to Run VBA Macros and Manage Documents
WPS Office offers robust support for VBA macros, allowing you to seamlessly write and run scripts to view document variables. With an intuitive interface and high compatibility with Word formats, automating tasks is easier than ever.
- 1. Open Your Document in WPS Writer: Launch WPS Office and open the document containing the variables you want to inspect.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon. If it is not visible, enable it from the application settings.
- 3. Open the VBA Editor: Click on the 'Macros' or 'Visual Basic' icon to launch the built-in VBA editor.
- 4. Insert and Run the Code: Add a new module, paste your document variable inspection code, and press Run to see the hidden data.

Frequently Asked Questions
Can I edit document variables using VBA?
Yes, you can modify an existing document variable by assigning a new value to it using the syntax: ActiveDocument.Variables("YourVariableName").Value = "New Value".
What are document variables typically used for?
Document variables provide a hidden storage area within a Word document to save custom data strings. They are commonly used by developers to pass and preserve data between external applications (like Microsoft Access or Excel) and Word macros.
Why can't I see the Developer tab to open the VBA editor?
The Developer tab is hidden by default in most word processors to simplify the interface for regular users. You can enable it by going to the software's Options or Preferences, selecting 'Customize Ribbon', and checking the box next to 'Developer'.
Will converting my document to a ZIP file corrupt my original file?
It will not corrupt your original file as long as you make a copy first and change the extension of the copy. Always ensure you are working on a duplicated version of the document when modifying file extensions.




