Field codes: IF field

The If field compares two values and then inserts the text appropriate to the result of the comparison. When used in a mail merge main document, 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.) can examine information in the merged data records (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.), such as postal codes or account numbers. For example, you can send letters to only those clients located in a particular city.

You can use the If field in a document, or you can use the If field as part of an If-Then-Else rule in a mail merge.

To use the If field in a document, do the following:

  1. On the Insert tab, in the Text group, click Quick Parts, and then click Field.
  1. In the Field names list, select If.
  2. In the Field codes box, type the expressions, operator, true text, and false text, following the syntax shown above.
  3. Click OK.

To use the If field as part of a mail merge, do the following while you are setting up the mail merge:

  1. On the Mailings tab, in the Write & Insert Fields group, click Rules, and then click If...Then...Else.
  1. In the Field name list, select the name of a merge field, such as City.
  2. In the Comparison list, select the operator that you want. For example, to single out recipients in a particular city, select Equal to.
  3. In the Compare to box, type the value that you want to use. For example, to single out recipients in Tokyo, type Tokyo.

     Note   If you selected is blank or is not blank in the Comparison list, leave the Compare to box empty.

  4. In the Insert this text box, type the text that you want to appear in the document when the condition that you specified is true. For example, to customize the text for recipients in Tokyo, you can type especially for your Tokyo neighborhood.
  5. In the Otherwise insert this text box, type the text that you want to appear in the document when the condition that you specified is not true. For example, to provide generic text for recipients who are not in Tokyo, you can type especially for your surrounding neighborhood.

Syntax

When you view the If field code in your document, the syntax looks like this:

{ IF Expression1 Operator Expression2 TrueText FalseText }

 Note   A field code tells the field what to show. Field results are what is shown in the document after having evaluated the field code. To toggle between viewing the field code and the field code results, press Alt+F9.

Instructions

Expression1, Expression2

Values that you want to compare. These expressions can be merge field data, bookmark (bookmark: A location or selection of text in a file that you name for reference purposes. Bookmarks identify a location within your file that you can later refer or link to.) names, strings of characters, numbers, nested fields that return a value, or mathematical formulas. If an expression contains spaces, enclose the expression in quotation marks.


 Notes 

  • Expression2 must be enclosed in quotation marks so that it is compared as a character string.
  • If the operator is = or <>, Expression2 can contain a question mark (?) to represent any single character or an asterisk (*) to represent any string of characters.
  • If you use an asterisk in Expression2, the portion of Expression1 that corresponds to the asterisk and any remaining characters in Expression2 cannot exceed 128 characters.

Operator

Comparison operator. Insert a space before and after the operator.

Operator Description
= Equal to
<> Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

TrueText, FalseText

Text that results when the comparison is true (TrueText) or false (FalseText). If FalseText isn't specified and the comparison is false, the IF field has no result. Each string containing multiple words must be enclosed in quotation marks.

Example

The following example specifies that if the customer order is greater than or equal to 100 units, the text "Thanks" appears in the document. If the customer order is fewer than 100 units, the text "The minimum order is 100 units" appears in the document.

{IF order>=100 "Thanks" "The minimum order is 100 units"}

Top of Page Top of Page

 
 
Applies to:
Word 2013, Word 2010, Word 2007