logo
search
VBA & Macro Problems

How to Use Word Objects Across Excel VBA Modules

Maira MehtabMaira Mehtab Sep 27, 2026 870 views

Question details

The user needs to make public Word application and document objects accessible when Excel VBA code is split across multiple modules.

Product
Excel VBA
Device & OS
not provided
Scenario
Writing Excel VBA macros that interact with Word documents across different code modules.
Observed behavior
Public Word object variables appear unavailable or throw reference errors when called from a different module than where they were declared.
Before you start

Ensure that you have enabled the Microsoft Word Object Library reference in your Excel VBA editor via Tools > References before attempting to declare and use Word objects.

Solution 1Recommended

Qualify Public Variables with the Module Name

Explicitly state the module name before the variable name to ensure the VBA compiler correctly locates the public object.

When VBA code is divided into multiple standard modules, global or public variables can sometimes lose context or become ambiguous. By qualifying the variable with its parent module name, you establish a direct reference path.

1
Declare the public variable

In your primary module (e.g., Module1), declare your Word application object at the top using the syntax: Public wdApp As Word.Application.

2
Qualify the variable in other modules

When referencing the wdApp variable in a different module (e.g., Module2), use the explicit syntax Module1.wdApp instead of just typing wdApp.

3
Apply to all object methods

Ensure that any properties or methods called on the object from outside its home module also use this qualified format, such as Module1.wdApp.Visible = True.

Write and Execute VBA Macros Seamlessly in WPS Office

WPS Office includes a robust built-in Macro editor that fully supports VBA. You can easily manage cross-module references, automate tasks, and interact with other document types using WPS Spreadsheet.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled workbook (.xlsm).
  2. 2. Access the Macro Editor: Navigate to the Developer tab on the ribbon and click on 'Visual Basic' or 'Macro Editor' to open the scripting environment.
  3. 3. Manage Modules: Right-click in the Project window to insert standard or class modules, and declare your public variables clearly.
  4. 4. Run Your Macros: Execute your automated workflows directly within WPS Office with high execution speed and stability.
Fully compatible with Microsoft Excel VBA (.xlsm) formats and syntaxBuilt-in Macro editor for advanced scripting and cross-module debuggingSeamless interaction between WPS Spreadsheet and WPS Writer objectsFree and lightweight Office suite for everyday automation
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a 'Variable not defined' error when accessing a public Word object?

This usually happens if the variable is declared using 'Dim' or 'Private' instead of 'Public', or if the referencing module does not explicitly qualify the variable with the originating module's name.

Do I need to enable specific references to use Word objects in Excel VBA?

Yes, to use early binding (e.g., declaring variables specifically as Word.Application), you must go to Tools > References in the VBA editor and check the 'Microsoft Word Object Library'.

When should I use a class module instead of a standard module for Word objects?

Use a class module when your project requires managing multiple, separate instances of Word applications. It allows you to encapsulate data and instantiate independent objects cleanly, avoiding the risks of global variable overrides.