How to prevent line breaks after an empty merge field


When you use the WordTemplate object to populate a WordWriter template document with data retrieved from a data source, you may have situations in which a merge field is not bound to a value. The most common scenario is creating a mailing address. Each field of a mailing address may be represented by a merge field. Typically, the second line of the address is optional. In the generated document, if the merge field corresponding to the second line of the address does not have a value, a blank line may be present.

In Microsoft Word, a blank line generated during a mail merge can be suppressed using application settings. With WordWriter, a blank line is not automatically removed.

WordWriter template document:

Generated document:


The following discussion uses a merge field called AddressLine2 as an example, but is applicable to any field with optional value.

In the WordWriter template document, if the line break is separate from the AddressLine2 merge field, the line break will be present in the generated document whether or not there is a value for AddressLine2.

In order to prevent a line break if AddressLine2 is not present, do not put a line break after the AddressLine2 merge field in the WordWriter template document. Instead, insert the line break only when the AddressLine2 is present. To do so, perform the following steps in Microsoft Word:

1. Remove the line break, if any, after the AddressLine2 merge field

2. In the WordWriter template document, insert a line break within the AddressLine2 data marker itself:

  • Right-click on the AddressLine2 merge field
  • Select Toggle Field Codes
  • After the merge field name, enter \f, followed by a space, double quote, then press Enter to get a new line, followed by another quote
  • If the merge field contains a * MERGEFORMAT string (i.e., the “Preserve formatting during updates” option is enabled), insert a space before it

3. You should see the following:

 { MERGEFIELD Address_Line_2 \f \* MERGEFORMAT } 

4. If you select “View Paragraph marks” (Tools, Options, View, Paragraph marks), you should see a paragraph mark (¶) in the field code as follows:

Each dot represents a space. Note that there is a space between each part of the merge field.

5. As a final check, if you right-click on the AddressLine2 merge field, then select Edit field, the “Text to be inserted after” option should be checked

After you follow the above steps, the address in the generated document should no longer contain a blank line, as shown in the below image:

Related posts: