VBA & Macro Problems

How to Populate MultiPage UserForm Controls from DocVariables in Word VBA

Jason MillerJason Miller Oct 26, 2026 869 views

Question details

The user needs to reload DocVariable values into checkbox controls on a multipage VBA UserForm when reopening a document, but encounters an 'Object has been deleted' error.

How to Populate MultiPage UserForm Controls from DocVariables in Word VBA
Product
Word
Device & OS
not provided
Scenario
Reloading saved document variables into UserForm controls upon opening a macro-enabled document or template.
Observed behavior
Using 'ThisDocument' or directly referencing 'ActiveDocument.Variables' inside a loop triggers an 'Object has been deleted' error, failing to populate the UserForm.
Before you start

Ensure your document is saved as a macro-enabled format (.docm) and that you have enabled macros in your Trust Center settings before running or debugging UserForm code.

Solution 1Recommended

Declare and Use an Explicit Document Object

Avoid variable reference errors by explicitly declaring a Document object instead of relying on ThisDocument, especially when code is stored in a template.

When VBA code is stored in a template but executed on a document created from it, using 'ThisDocument' refers to the template itself, not the active document. This discrepancy often causes the 'Object has been deleted' error.

By explicitly setting a Document object to the ActiveDocument, you ensure the code retrieves DocVariables from the correct file.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic Editor and locate the initialization code for your UserForm.

2
Declare the Document Object

Declare a specific Document object at the beginning of your routine by typing: Dim DocSource As Document.

3
Assign the Active Document

Set the newly declared object to the current document being edited by adding: Set DocSource = ActiveDocument.

4
Update Control Loops

Modify your UserForm control loop to reference variables through this specific object: DocSource.Variables(variableName).Value.

Declare and Use an Explicit Document Object
Tip: If you are opening a separate document via code to extract variables, use Set DocSource = Documents.Open("C:\Path\To\Doc.docm") instead.
Advanced Macro Support

Create and Manage UserForms with WPS Writer

WPS Office provides robust support for VBA macros, allowing you to create complex UserForms, manage DocVariables, and automate your document workflows seamlessly without compatibility issues.

  1. 1. Enable Developer Tools: Open your document in WPS Writer, go to the Options menu, and ensure the 'Developer' tab is enabled.
  2. 2. Access the VBA Editor: Click on the 'Developer' tab and select 'Visual Basic' to launch the integrated macro editor.
  3. 3. Implement UserForm Code: Insert or modify your UserForm and apply the explicit Document object code to load your variables safely.
  4. 4. Save with Macros: Save your file as a WPS Macro-Enabled Document to ensure all UserForm data and scripts are preserved.
Full compatibility with Microsoft Word VBA macros and DocVariablesIntuitive Visual Basic for Applications (VBA) editor includedLightweight application with high performance for automated tasksNative support for macro-enabled document formats (.docm, .dotm)
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get an 'Object has been deleted' error in Word VBA?

This error frequently occurs when referencing ThisDocument inside a template-based macro while trying to access variables in the active document. Explicitly setting ActiveDocument to a declared Document object resolves this.

What is the difference between ActiveDocument and ThisDocument in VBA?

ActiveDocument refers to the document currently in focus and being edited by the user. ThisDocument strictly refers to the document or template where the executing VBA code is physically stored.

How do I check if a DocVariable exists before calling it in VBA?

You can iterate through the Document.Variables collection using a For Each loop to compare names, or safely wrap the variable call in an 'On Error Resume Next' block to handle instances where the DocVariable has not been created yet.