Skip to main content

How to create conditional section / paragraph in a Word merge document

When using a Word Merge document you have the option of including Optional Sections controlled by conditions. Conditional paragraphing is where a particular paragraph or section appears in the merge d

Updated over 2 weeks ago

When using a Word Merge document you have the option of including Optional Sections controlled by conditions. Conditional paragraphing is where a particular paragraph or section appears in the merge document only when a condition is met.

Below is an example with 4 different possible outcomes

mceclip0.png

The paragraphs in the example above would only appear to the conditions set in the report. Conditions for the conditional paragraphs can be defined using SQL code using a Custom SQL field like for HOST_COUNTRY. Here is an example of the user report.

In the Merge Document, to start a section you must use the Section start identifier and condition using the following format: EQStart:SectionName FIELD='Value’.

For example, place the cursor at the start of the section. Create a Merge Tag, using the Field window and in the Field name: field, enter the EQStart:1 HOST_COUNTRY='USA' where 1 is the SectionName, HOST_COUNTRY is the FIELD from the User Report. Since the comparison Value is a string. it is enclosed in single quotes e.g. 'USA. If the string must include a single quote then use two single quotes in the value, double quotes can be used without modification.

mceclip2.png

Press OK when done.

To end the optional section, place the cursor at the end of the section and create a Merge Tag using the Section End identifier. This uses the following format “EQEnd:SectionName”.

mceclip3.png

Please note that there are no spaces on either side of the colon (:) and there is a space at the end of the Section Name before the Condition.

Start and End Merges Tags have been highlighted in yellow to show how they appear.

mceclip5.png

Repeat for the remaining conditional paragraphs. Click here for the sample file.

mceclip6.png

Advanced settings for Optional Sections

To properly set up optional sections inside a repeatable region, you must nest a merge field inside the optional region merge field. In order to do that, you need to hit Alt-F9 to see the merge field in its expanded view, which looks like this:

{MERGEFIELD “EQStart:One EMPLOYEE_FULL_NAME=’BOB’” \* MERGEFORMAT}

Next, replace EMPLOYEE_FULL_NAME with a pair of single-quotes:

{MERGEFIELD “EQStart:One ‘’=’BOB’” \* MERGEFORMAT}

Next, position the cursor between the single quotes, and insert another merge field:

{MERGEFIELD “EQStart:One ‘{MERGEFIELD EMPLOYEE_FULL_NAME \* MERGEFORMAT}’=’BOB’” \* MERGEFORMAT}

End by using the EQEnd: merge tag as normal.

If it is possible that the value in the nested merge field (EMPLOYEE_FULL_NAME, in this example) might contain any single-quotes (as in an apostrophe in the name), then you must create a calculated column in the user report that will replace that single quote with two single quotes right next to each other (escaping it, so the parser doesn’t think that single quote is the end of the string). You can do that with a SQL calculated column like this:

REPLACE([TABLE: FIELD],'''','''''')

(Replace the [TABLE: FIELD] with the appropriate field from the report).

Did this answer your question?