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.

Click to OpenOpen 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.)

Click to OpenCreate or Edit a Document

Open your Mail Merge Documents editor, then choose an existing document and click Open, or click New to create a new document. You can create documents with either Microsoft Word or with HTML. Existing documents created in Word are preceded by the  Word icon. HTML documents are indicated with your primary browser icon (i.e.  ).

Click to OpenAdd "If" Conditional Statements

Toggle Merge Code DisplayClosed - 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. (HTML documents always display the inserted codes.)

  1. Click to OpenPosition Your Cursor

    Place your cursor in the document where you want the information to print.

  2. Click to OpenOpen "Field" Menu

    Open the Field option from the Insert menu bar of your Word window.

    Word 2002/2003Closed - Click the Insert menu and select Field. (No illustration available.)

    Word 2007 or greaterClosed - Click the Insert ribbon bar, and select Quick Parts. Then choose the Field option.


  3. Click to OpenInsert "IF" Field

    Select Mail Merge in the Category box, and choose the IF field name option. Click Ok to insert an empty merge code command into your document.

    If you do not see the merge code, or if you see an error, press Alt+F9 to toggle the merge code display.

  4. Click to OpenRemove Extraneous Code

    Remove \*MERGEFORMAT from the code. The code will now look like this: {IF}

  5. Click to OpenInsert the Variable to Test

    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.

  6. Click to OpenInsert the Operator

    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 ( = )Closed - This operator includes records that exactly match the field value.

    Not equal to ( <> )Closed - This operator excludes records that match the field value.

    Greater than (> )Closed - This operator includes records whose value is more than, but not the same as, the field value.

    Greater than or equal to ( >= )Closed - This operator includes records whose value is either more than or the same as the field value.

    Less than ( < )Closed - This operator includes records whose value is less than, but not the same as, the field value.

    Less than or equal to ( <= )Closed - 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} = }

  7. Click to OpenInsert the Value to Test

    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} }

  8. Click to OpenAdd 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 CodesClosed - 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.

  9. Click to OpenAdd 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 CodesClosed - 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.

  10. Click to OpenProofread

    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 to OpenSave and Exit

Click the X in the upper right corner of the document window. As you exit, you have the opportunity to save the document.

Click to OpenExample IF statements

You can control your mail merge document output with conditional statements. These statements may be more or less elaborate as needed.

Click to OpenCheck for 2nd Address Line

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}

Click to OpenNested Conditions

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. "}"}

Click to OpenCondition Stringing

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...