Document Editing Problems

How to Compare Word Document Variables and Hide Duplicate Address Lines

Huma Ashraf ChHuma Ashraf Ch Oct 29, 2026 868 views

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.

How to Compare Word Document Variables and Hide Duplicate Address Lines
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.
Before you start

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.

Solution 1Recommended

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.

1
Insert a blank field

Press Ctrl + F9 to insert a blank field code brace {}. Do not type the braces manually using your keyboard.

2
Type the IF statement

Inside the braces, construct your IF logic: { IF { DOCVARIABLE Address1 } <> { DOCVARIABLE Address2 } "{ DOCVARIABLE Address2 }" "" }.

3
Handle paragraph breaks

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.

4
Update the fields

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 an IF Field to Conditionally Hide Duplicate Address Lines
Field Code Syntax: Always use Ctrl + F9 to create nested braces for the DOCVARIABLE parts. Manual curly brackets will not be recognized as valid fields.
Efficient Document Formatting

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. 1. Open your document: Launch WPS Writer and open the template where you want to add the conditional address lines.
  2. 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. 3. Build your logic: Enter your IF and DOCVARIABLE logic inside the field brackets just as you would in Microsoft Word.
  4. 4. Update the result: Right-click the inserted field and choose 'Update Field', or press F9, to apply the conditional formatting.
100% compatible with Microsoft Word field codes and syntaxIntuitive interface for inserting and updating dynamic fieldsFree and lightweight alternative for professional document editingRobust support for VBA macros and complex mail merge logic
microsoft office alternative - wps office

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.