How to Clear Date Fields in Multiple Word Documents Using VBA
Question details
The user needs to use VBA to evaluate a date value from a UserForm and conditionally clear the date field in one document while retaining it in another based on a specific cutoff date.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Automating the population and modification of date fields across two separate Word documents simultaneously using a VBA macro.
- Observed behavior
- Instead of using fragile bookmarks to target document locations, the goal is to implement robust DOCVARIABLE fields and use conditional logic to assign or clear values without triggering Null assignment errors.
Before modifying your VBA script, ensure the Developer tab is enabled in your ribbon and make a backup copy of your target documents to prevent accidental data loss during testing.
Use DOCVARIABLE Fields and DateDiff in VBA
Implement Document Variables instead of bookmarks, and use an If...Then...Else condition to clear variables with a single space when they meet the cutoff criteria.
DOCVARIABLE fields offer a much more stable alternative to standard bookmarks. Bookmarks can easily be deleted or overwritten by users when editing a document. Document variables, on the other hand, are hidden data containers stored within the document's structure.
When attempting to clear a DOCVARIABLE, remember that VBA cannot assign a Null value to a Document Variable. Attempting to do so will result in an error. To successfully blank out the field, you must assign a single space string to the variable's value.
In your Word documents, press Ctrl + F9 to insert field braces, and type DOCVARIABLE followed by your variable name (e.g., { DOCVARIABLE TextBoxx25 }). Press F9 to update the field.
Press Alt + F11 to open the Visual Basic for Applications window and locate the code block tied to your UserForm or command button.
Use an If...Then...Else statement with DateDiff to compare your form input against the cutoff date. For example: If DateDiff("d", Me.TextBoxx25Text, "11/12/2020") > 0 Then...
Inside your conditional statement, clear the target variable by assigning a single space. For example: Doc1.Variables("TextBoxx25").Value = " ".
Ensure the changes reflect in the document by appending field update commands at the end of your script: Doc1.Range.Fields.Update and Doc2.Range.Fields.Update.

Automate Document Variables with WPS Writer
WPS Office supports VBA macros in its advanced editions, allowing you to execute identical scripts to automate field updates, compare dates, and seamlessly manage variables across multiple documents.
- 1. Launch WPS Writer: Open WPS Office, start WPS Writer, and open the multiple documents you wish to automate.
- 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon and click on the 'Macros' or 'Visual Basic' icon to launch the editor.
- 3. Implement Your VBA Script: Paste your standard VBA code using the DOCVARIABLE and If...Then...Else logic into the module.
- 4. Run and Automate: Execute the macro to instantly evaluate the dates and update the document variables across your active WPS Writer documents.

Frequently Asked Questions
Why should I use DOCVARIABLE instead of Bookmarks in VBA?
Bookmarks are fragile and can be easily deleted or corrupted if a user accidentally types over them or if text is replaced improperly. DOCVARIABLE fields store data internally in the document properties, making them highly resilient and much easier to manage for repeated automated updates.
How do I clear a DOCVARIABLE in VBA without triggering an error?
A document variable cannot be explicitly set to Null or an entirely empty uninitialized state without causing a runtime error. To make the field appear completely blank in the text, you should assign a single space string (" ") to the variable's value property.
How do I ensure VBA date comparisons are not affected by regional settings?
Relying on string dates like "11/12/2020" can cause VBA to misinterpret the month and day depending on the system's local date format (MM/DD vs DD/MM). To guarantee accuracy, construct your comparison date using the DateSerial function, such as DateSerial(2020, 11, 12).




