Fix Word COMPARE Field Errors with DOCVARIABLE Values
Question details
The user needs to resolve an issue where the Word COMPARE field evaluates incorrectly when DOCVARIABLE values contain special characters.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Using the COMPARE field to evaluate DOCVARIABLE values containing hash (#) symbols or other special characters.
- Observed behavior
- The COMPARE field returns incorrect comparison results due to the way document variables handle specific special characters.
Ensure your document variables are correctly defined and that you have enabled the display of field codes (Alt + F9) to view and manually edit the COMPARE and DOCVARIABLE syntax.
Enclose DOCVARIABLE Values in Quotation Marks
Adding quotation marks around the document variables forces Word to treat the special characters as literal strings, resolving the incorrect comparison.
This is the most reliable workaround for string storage and retrieval bugs in Microsoft Word document variables.
Press Alt + F9 on your keyboard to reveal the field codes in your document.
Locate your COMPARE field and manually type double quotation marks around the DOCVARIABLE field. It should look like this: { COMPARE "{ DOCVARIABLE MyVar }" "Value" }.
Select the modified field, press F9 to update it, and press Alt + F9 again to hide the codes and view the correct result.

Submit Feedback to Office VBA Support
Since this is a known string retrieval issue in Microsoft 365 build versions, reporting it helps developers investigate the root cause.
Try WPS Office for a Smoother Document Editing Experience
If you frequently encounter field evaluation bugs in Microsoft Word, WPS Office offers a free, lightweight, and highly compatible alternative. Enjoy seamless document creation and editing with a familiar user interface.
- 1. Download WPS Office: Visit the official WPS website to download the free office suite.
- 2. Install and Launch: Run the installer and open WPS Writer.
- 3. Open Your Document: Open your existing Word document in WPS Office and continue editing seamlessly.

Frequently Asked Questions
Why does the Word COMPARE field fail with hash symbols?
The error occurs because of a bug in how Microsoft Word stores and retrieves strings from document variables when they contain certain special characters, causing the COMPARE field to misinterpret the value.
How do I edit field codes in my document?
You can view and manually edit field codes by pressing Alt + F9 on Windows (or Option + F9 on Mac). This reveals the underlying syntax of your COMPARE and DOCVARIABLE fields.
Does unlinking the DOCVARIABLE fix the comparison error?
No, unlinking the DOCVARIABLE fields alone may still return incorrect results (such as returning True when it should be False). The most reliable workaround is enclosing the values in quotation marks.




