VBA & Macro Problems

How to Share Word VBA Document Variables Between UserForms

Emma BrownEmma Brown Nov 3, 2026 870 views

Question details

The user needs to share document objects between multiple Word VBA UserForms without relying on hard-coded file paths.

How to Share Word VBA Document Variables Between UserForms
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.
Before you start

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.

Solution 1Recommended

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.

1
Insert a Standard Module

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.

2
Declare Public Document Variables

At the very top of the newly created module (in the General Declarations section), type: 'Public Doc1 As Document' and 'Public Doc2 As Document'.

3
Remove Conflicting Local Variables

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.

4
Assign and Reference the Documents

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.

Declare Public Variables in a Standard Module
Scope Conflict Warning: Always double-check that you do not have identical variable names declared both at the module level and the procedure level to prevent unexpected 'Object Required' errors.
Free Microsoft Office alternative

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.

Fully compatible with Microsoft Word (.docx, .docm) formats.Familiar user interface that requires zero learning curve.Lightweight application that loads fast and saves system resources.Free to use for everyday document creation, editing, and sharing.
microsoft office alternative - wps office

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.