Document Editing Problems

Fix Word COMPARE Field Errors with DOCVARIABLE Values

Camila MilosovichCamila Milosovich Oct 29, 2026 870 views

Question details

The user needs to resolve an issue where the Word COMPARE field evaluates incorrectly when DOCVARIABLE values contain special characters.

Fix Word COMPARE Field Errors with DOCVARIABLE Values
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.
Before you start

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.

Solution 1Recommended

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.

1
Toggle Field Codes

Press Alt + F9 on your keyboard to reveal the field codes in your document.

2
Edit COMPARE Syntax

Locate your COMPARE field and manually type double quotation marks around the DOCVARIABLE field. It should look like this: { COMPARE "{ DOCVARIABLE MyVar }" "Value" }.

3
Update Fields

Select the modified field, press F9 to update it, and press Alt + F9 again to hide the codes and view the correct result.

Enclose DOCVARIABLE Values in Quotation Marks
Workaround Successful: This method prevents the hash symbol from breaking the string evaluation and returns the correct True or False value.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website to download the free office suite.
  2. 2. Install and Launch: Run the installer and open WPS Writer.
  3. 3. Open Your Document: Open your existing Word document in WPS Office and continue editing seamlessly.
Free and lightweight office suiteExcellent compatibility with Microsoft Word formats (.docx)Familiar interface for zero learning curveRobust handling of text and complex document formatting
microsoft office alternative - wps office

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.