How to Compare Word Document Variables and Hide Duplicate Address Lines
Question details
The user needs to compare two document variables representing address lines and automatically hide the second one if its value is identical to the first.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Formatting mail merges, invoices, or automated document templates with multi-line address blocks where duplicate lines might occur.
- Observed behavior
- Duplicate address lines are displayed when the values of two document variables (e.g., Address1 and Address2) are identical, requiring a method to conditionally hide the redundant line.
Ensure you have enabled field codes visibility in your document by pressing Alt + F9, which will allow you to easily insert and edit IF statements.
Use an IF Field to Conditionally Hide Duplicate Address Lines
The most straightforward approach is to use Word's built-in IF field to compare the two document variables and only display the second address line if it differs from the first.
This method uses a standard IF...THEN...ELSE field construction directly in your document. When the document updates its fields, it checks if Address1 and Address2 are the same. If they are, it outputs nothing; if they are different, it displays Address2.
Press Ctrl + F9 to insert a blank field code brace {}. Do not type the braces manually using your keyboard.
Inside the braces, construct your IF logic: { IF { DOCVARIABLE Address1 } <> { DOCVARIABLE Address2 } "{ DOCVARIABLE Address2 }" "" }.
To prevent an empty blank line from appearing, include the paragraph return inside the quotation marks of the true condition rather than outside the field.
Press Alt + F9 to toggle the field codes off and view the result, then select the text and press F9 to update the fields.

Use VBA to Delete Duplicate Document Variables
If you populate the document using VBA, you can handle the comparison directly in your code. Assigning an empty string to a document variable effectively deletes it, automatically removing the duplicate.
Manage Document Fields Easily with WPS Writer
WPS Writer fully supports advanced field codes like IF and DOCVARIABLE, allowing you to easily manage dynamic text, mail merges, and automated templates with a highly compatible interface.
- 1. Open your document: Launch WPS Writer and open the template where you want to add the conditional address lines.
- 2. Insert field codes: Go to the 'Insert' tab, click on 'Quick Parts', and select 'Fields' to access the field dialogue, or simply press Ctrl + F9.
- 3. Build your logic: Enter your IF and DOCVARIABLE logic inside the field brackets just as you would in Microsoft Word.
- 4. Update the result: Right-click the inserted field and choose 'Update Field', or press F9, to apply the conditional formatting.

Frequently Asked Questions
Why isn't my IF field hiding the duplicate text?
Make sure you pressed Ctrl + F9 to insert the field braces {}. Typing standard curly braces on your keyboard will not be recognized as a field code. Additionally, ensure you have updated the field by selecting it and pressing F9.
Does assigning a space delete a document variable in VBA?
No. Assigning a space (" ") simply changes the variable's value to a space character. To completely delete the document variable using VBA, you must assign it an empty string ("").
How can I view the hidden field codes in my document?
You can toggle the visibility of all field codes in your document by pressing Alt + F9. This reveals the underlying code, such as your IF statements and DOCVARIABLE fields, instead of their final calculated results.
How do I prevent a blank line when an address variable is hidden?
Place the paragraph break (Enter key) inside the quotation marks of the IF field's 'true' condition. This way, the paragraph break is only inserted if the address line is actually printed.




