VBA & Macro Problems

How to View and List Word Document Variables Using VBA

Huda QurayshiHuda Qurayshi Oct 22, 2026 870 views

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.

How to View and List Word Document Variables Using VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications window.

2
Insert a New Module

Click on 'Insert' in the top menu and select 'Module' to create a new blank script window.

3
Enter the VBA Code

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

4
Run the Macro

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.

Use VBA Code to Enumerate Document Variables
Quick Inspection: If you have many variables, consider modifying the code to output the results using Debug.Print to list them in the Immediate Window instead of clicking through multiple message boxes.
Advanced Document Management

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. 1. Open Your Document in WPS Writer: Launch WPS Office and open the document containing the variables you want to inspect.
  2. 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. 3. Open the VBA Editor: Click on the 'Macros' or 'Visual Basic' icon to launch the built-in VBA editor.
  4. 4. Insert and Run the Code: Add a new module, paste your document variable inspection code, and press Run to see the hidden data.
Seamlessly execute VBA macros and automation scriptsExcellent compatibility with Microsoft Word formats (.doc, .docx, .docm)Lightweight installation with fast performanceFree to download and use for daily office tasks
microsoft office alternative - wps office

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.