Applies ToWord for Microsoft 365 Word for Microsoft 365 for Mac Word 2021 Word 2021 for Mac Word 2019 Word 2019 for Mac Word 2016

The Compare field code compares two values and displays the result "1" if the comparison is true or "0" (zero) if the comparison is false. You can use this field to create compound logical comparisons with And and Or functions in an = (Formula) field, and then you can use the result of the = (Formula) field in an If field.

Syntax

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

{ COMPARE Expression1 Operator Expression2 }

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 to compare. Expressions can be bookmark names, strings of text, numbers, nested fields that return a value, or mathematical formulas. If an expression contains spaces, enclose the expression in quotation marks.

Operator

Comparison operator. Insert a space both 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

Note: 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. The expression must be enclosed in quotation marks so that it is compared as a character string. If you use an asterisk in Expression2, the portion of Expression1 that corresponds to the asterisk, plus any remaining characters in Expression2, cannot exceed 128 characters.

Examples

Suppose that the If field in the following example is inserted into a mail merge main document. The Compare fields examine the data fields CustomerNumber and CustomerRating as each data record is merged. The Or function of the = (Formula) field returns the value "1" (true) if at least one of the data fields indicates poor credit, in which case the first text in quotation marks is printed.

{ IF { = OR ( { COMPARE { MERGEFIELD CustomerNumber } >= 4 }, { COMPARE { MERGEFIELD CustomerRating } <= 9 } ) } = 1 "Credit not acceptable" "Credit acceptable"}

The following Compare field results in the value "1" if any value in the PostalCode data field is the range 98500 – 98599:

{ COMPARE " { MERGEFIELD PostalCode } " = "985*" }

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.