How to Share Word VBA Document Variables Between UserForms
Question details
The user needs to share document objects between multiple Word VBA UserForms without relying on hard-coded file paths.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Developing a Word macro where multiple UserForms require read and write access to the same opened document objects.
- Observed behavior
- Variables declared locally in one UserForm cannot be accessed by another, or local variables in procedures are hiding globally intended variables.
Ensure your Microsoft Word document is saved as a Macro-Enabled Document (.docm) and that you have the VBA Editor open with all your UserForms accessible.
Declare Public Variables in a Standard Module
By declaring document variables as Public in a standard module, you make them accessible globally across all UserForms and code modules.
In VBA, the scope of a variable determines where it can be read or modified. Variables declared with 'Dim' inside a UserForm or a button click event are local to that specific form or procedure.
To share a Document object across multiple UserForms, you must declare it as 'Public' in a standard code module. Additionally, you must be careful to remove any local declarations with the same name, as they will override (or 'shadow') your public variables.
In the VBA Editor, right-click your project in the Project Explorer window, hover over 'Insert', and click 'Module' to create a new standard module.
At the very top of the newly created module (in the General Declarations section), type: 'Public Doc1 As Document' and 'Public Doc2 As Document'.
Open the code behind your UserForms (e.g., your CommandButton click procedures). Look for and delete any lines that say 'Dim Doc1 As Document' or 'Dim Doc2 As Document'. Leaving these will cause the local variable to hide the public one.
In your first UserForm, assign the opened document to the variable using the Set keyword: 'Set Doc1 = Documents.Open("FilePath")'. You can now directly reference 'Doc1' in your second UserForm without redefining it.

Enhance Your Productivity with WPS Office
Dealing with complex VBA scoping rules in Microsoft Word can be frustrating. Consider switching to WPS Office, a highly compatible, free, and lightweight alternative that simplifies your daily document workflows while fully supporting standard Microsoft Office formats.

Frequently Asked Questions
Why do I get an 'Object variable or With block variable not set' error in my second UserForm?
This usually happens if the public variable was never assigned a value using the 'Set' keyword in the first UserForm, or if a local variable with the same name shadowed the public assignment, leaving the public variable empty.
Can I pass a variable between UserForms without using a standard module?
Yes, you can pass variables directly by creating a Public Property or a Public Subroutine in the second UserForm that accepts a Document object as an argument, and then calling it from the first UserForm before showing the second one.
What is the difference between 'Public' and 'Global' in VBA?
In standard modules, 'Public' and 'Global' function identically when declaring variables. However, 'Public' is the modern standard in VBA and is highly recommended, while 'Global' is a legacy keyword retained for backward compatibility.




