Examples of IF fields

The IF field (field: A set of codes that instructs Microsoft Word to insert text, graphics, page numbers, and other material into a document automatically. For example, the DATE field inserts the current date.) performs one of two alternative actions, depending on a condition you specify. For example, the statement "If the weather is sunny, we'll go to the park; if not, we'll go to the movies" specifies a condition that must be met (sunny weather) for a certain action to take place (going to the park). If the condition is not met, an alternative action occurs (going to the movies).

The following IF field is a similar type of statement. If the current data record (data record: A complete set of related information that corresponds to one row of information in the data source. All information about one client in a client mailing list is an example of a data record.) contains "San Francisco" in the City field, Microsoft Word prints the first text in quotation marks in the merged document that results from that data record. Otherwise, Word prints the second set of text. This example uses the IF and MERGEFIELD fields together.

{ IF { MERGEFIELD City } = "San Francisco" "Please call our local office." "Please return the enclosed form in the provided envelope." }

Word inserts an IF field in this form when you click the Insert Word Field button on the Mail Merge toolbar (toolbar: A bar with buttons and options that you use to carry out commands. To display a toolbar, press ALT and then SHIFT+F10.) and then click If...Then...Else.

Here are some examples of ways to modify the IF field codes (field code: Placeholder text that shows where specified information from your data source will appear; the elements in a field that generate a field's result. The field code includes the field characters, field type, and instructions.) 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 (data field: A category of information that corresponds to one column of information in a data source. The name of each data field is listed in the first row (header row) of the data source. "PostalCode" and "LastName" are examples of data field names.) 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." " "} "}

 
 
Applies to:
Word 2003