Examples of IF fields

Microsoft Office Word 2003

toolbar and then click If...Then...Else.

Here are some examples of ways to modify the IF field codes by using them in concert with other field codes:

ShowCompare a data field with another data field or bookmark

This IF field compares the number in the Rate data field with the number in the Discount data field. If the numbers are equal, the specified text is printed.

{ IF { MERGEFIELD Rate } = { MERGEFIELD Discount } "We will offer you a larger discount. " " " }

(If number in the Rate data field does not equal the number in the Discount field, no text is printed.)

ShowInsert other Word fields into an IF field

Example 1

If the condition specified by this IF field is met, the INCLUDETEXT field inserts the contents of the indicated document. To specify the location and file name of the included document, use double backslash (\\) characters, as shown in the example. If the condition is not met, Word inserts the AutoText entry "Copyright."

{ IF { MERGEFIELD State } = "NY" "{ INCLUDETEXT c:\\worddocs\\contract.doc }" "{ AUTOTEXT Copyright }" }

Example 2

If the Company data field for a data record contains any information— that is, if the field is not blank— Word prints text from the CompanyAddress field. Otherwise, Word prints text from the HomeAddress field.

{ IF { MERGEFIELD Company } <> " " "{ MERGEFIELD CompanyAddress }" "{ MERGEFIELD HomeAddress }" }

ShowPerform calculations in an IF field

In this example, the first = (Formula) field in the following example computes the current balance of each account. If a balance remains, the Formula field is repeated to report the current balance. If the computed balance is 0 (zero) or less, Word prints the alternative text, "Your account is paid in full. Thank you."

{ IF { = { MERGEFIELD Balance } - {MERGEFIELD Payment } } > 0 "Your current balance is { = { MERGEFIELD Balance } - { MERGEFIELD Payment } \# "$#,##0.00; " }." "Your account is paid in full. Thank you." }

ShowSpecify multiple conditions

You can specify multiple conditions by nesting a COMPARE field or another IF field inside an IF field.

Example 1

In this example, COMPARE fields examine the data fields CustomerNumber and CustomerRating as Word merges each data record. The AND function of the = (Formula) field returns a value of "1" (true) if both of the data field values indicate a satisfactory account, in which case Word prints the first text in quotation marks.

{ IF { = AND ( { COMPARE { MERGEFIELD CustomerNumber } >= 4 }, { COMPARE { MERGEFIELD CustomerRating } <= 3 } ) } = 1 "Satisfactory" "Unsatisfactory"}

Example 2

In this example, special rates are offered only to residents of California or Washington. If the State field specifies any other state, no additional text is printed because no alternative text has been specified in the second IF field.

{IF {MERGEFIELD State} = "CA" "For California residents, we offer special rates to Asia and Japan." "{IF {MERGEFIELD State} = "WA" "For Washington residents, we offer special rates to Asia and Japan." " "} "}