Mail Merge "If" Conditional Statements
Use the mail merge If conditional code to test the data being merged, and insert appropriate wording into the document. For example, you may want to print specific paragraphs based on the patient’s age, or include a second address line on envelopes only when appropriate.
Open the Mail Merge Documents Editor
Select Editors from the Tools section of the Home ribbon bar. Then, from the General section, open the Mail Merge Documents editor. (You can also use the Search field to look for the editor you want to work with.)
Open your Word icon. HTML documents are indicated with your primary browser icon (i.e.
).
Add "If" Conditional Statements
Toggle Merge Code Display - When creating and editing word processing documents with mail merge codes (such as variables and conditional statements), it is helpful and sometimes necessary to be able to see the codes. During regular daily use, however, you may wish to have them off. While different versions of Word have different ways of accessing this setting, an easy way to toggle the merge code display, regardless of your version of Word, is to press Alt+F9 on your keyboard.
-
Place your cursor in the document where you want the information to print.
-
Open the Field option from the Insert menu bar of your Word window.
Word 2002/2003
- Click the Insert menu and select Field. (No illustration available.)
Word 2007 or greater
- Click the Insert ribbon bar, and select Quick Parts. Then choose the Field option.
-
Remove \*MERGEFORMAT from the code. The code will now look like this: {IF}
-
Position your cursor after the IF, but inside the braces, and insert the variable you want to test. (See "Mail Merge Variables" for details.) The code will now look something like this, depending on the variable you inserted:
{IF {MERGEFIELD Patient_Age_Years} }
{IF {MERGEFIELD Patient_Full_Name} }
Notice the two braces at the end of the code. The first brace ends the variable code, the second brace ends the IF code.
-
Add a mathematical operator after the variable to specify how you want to test the variable's value. Be sure to have a space both before and after the operator.
Equal to ( = )
- This operator includes records that exactly match the field value.
Not equal to ( <> )
- This operator excludes records that match the field value.
Greater than (> )
- This operator includes records whose value is more than, but not the same as, the field value.
Greater than or equal to ( >= )
- This operator includes records whose value is either more than or the same as the field value.
Less than ( < )
- This operator includes records whose value is less than, but not the same as, the field value.
Less than or equal to ( <= )
- This operator includes records whose value is either less than or the same as the field value.
{IF {MERGEFIELD Patient_Age_Years} >= }
{IF {MERGEFIELD Patient_Full_Name} = }
-
Add the value, or another variable, you want to use to compare with the first variable. Values you type (such as a number or specific text) should be enclosed in quotation marks. Values you insert as variables should not include quotation marks. Again, be sure to include leading and following spaces between each portion of the code.
In this example, we are testing whether the patient is over 18 years old. The specific value being tested, 18, is enclosed in quotation marks:
{IF {MERGEFIELD Patient_Age_Years} >= "18" }
In this example, we are testing whether the patient is also the responsible party. Since the value we are testing is another variable, the responsible party's name, it is not enclosed in quotation marks.
{IF {MERGEFIELD Patient_Full_Name} = {MERGEFIELD Resparty_Full_Name} }
-
Add Text For the Value Being True
Enter the text and variables to print if the condition is met, enclosed in quotation marks, leaving a space at the end.
In this example, we add a sentence about adult patients if the patient age is found to be 18 years old or older:
{IF {MERGEFIELD Patient_Age_Years} >= "18" "We recommend other treatments for our adult patients" }
In this example, we add a sentence appropriate for the patient and responsible party being the same person:
{IF {MERGEFIELD Patient_Full_Name} = {MERGEFIELD Resparty_Full_Name} "We look forward to seeing you." } }
Using Quotes Inside Merge Codes
- If you want quotation marks within the text you want printed, you cannot simply type them. You must insert them as Quote field codes. Consult your Word documentation or please contact Ortho2 for further information.
-
Add Text For the Value Being False
Enter the text and variables to print if the condition is not met, enclosed in quotation marks. If you do not want anything printed when the condition is false, skip this step.
In this example, we don't want any extra text if the patient is under 18 years old, so we didn't add anything after the quotation mark:
{IF {MERGEFIELD Patient_Age_Years} >= "18" "We recommend other treatments for our adult patients" }
In this example, we add alternate text appropriate for when the patient and responsible party are different people:
{IF {MERGEFIELD Patient_Full_Name} = {MERGEFIELD Resparty_Full_Name} "We look forward to seeing you." "We look forward to seeing you and {MERGEFIELD Patient_Nick_Name." }
Using Quotes Inside Merge Codes
- If you want quotation marks within the text you want printed, you cannot simply type them. You must insert them as Quote field codes. Consult your Word documentation or please contact Ortho2 for further information.
-
Be sure to look over your conditional statements carefully to check for spelling errors, and to be sure all quotes and braces are correctly placed, and come in pairs. Any missing or misspelled code will cause the statement to fail when merged.
Click the X in the upper right corner of the document window. As you exit, you have the opportunity to save the document.
You can control your mail merge document output with conditional statements. These statements may be more or less elaborate as needed.
Use conditional statements to check for an address second line, and ensure that there will no be a blank line in the letter or on the envelope when there is no second line. The example below tests to see if the variable is “not equal to nothing,” in other words that it is not blank. This concept can be applied in other conditional statements.
{MERGEFIELD resparty_full_name}{MERGEFIELD resparty_address_1}{IF {MERGEFIELD resparty_address_2} <> "" "{MERGFIELD resparty_address_2}"}{MERGEFIELD resparty_city}, {MERGEFIELD resparty_state} {MERGEFIELD resparty_zip}
You can nest conditional statements within other conditional statements.
{IF {MERGEFIELD patient_userdef1} = "left" "We love our left-handed patients. " "{IF {MERGEFIELD patient_userdef1} = "right" "We love our right-handed patients. " "We can’t wait to meet you and see which hand you use. "}"}
If you are certain that no records will have a blank field for your variable, or if you desire the document to print nothing when it is blank, you can simply string conditional statements together rather than nesting them to make the form letter a little simpler to read.
This example is a string of individual conditional statements:
{IF {MERGEFIELD doctor_abbr} = "JS" "Dr. Smith"}{IF {MERGEFIELD doctor_abbr} = "CJ" "Dr. Jones"}{IF {MERGEFIELD doctor_abbr} = "AB" "Dr. Brown"} looks forward to meeting you.
This is the same result, using nested conditional statements:
{IF {MERGEFIELD doctor_abbr} = "JS" "Dr. Smith" "{IF {MERGEFIELD doctor_abbr} = "CJ" "Dr. Jones" "{IF {MERGEFIELD doctor_abbr} = “AB” “Dr. Brown”}”}”} looks forward...