How to compare many values with an OR statement in Word mergefields?

Solution 1:

If the v1, v2 are MERGE fields, in the general case you need { MERGEFIELD v1 } and { MERGEFIELD v2 }. You can then do an OR using a construct like this:

{ IF { =OR({ COMPARE { MERGEFIELD v1 } = 0 },{ COMPARE { MERGEFIELD v2 = 0 }) } = 0 "Lorem Ipsum.." }

(As usual, all the { } have to be the special field code brace pairs that you can insert on Windows Word using ctrl-F9.

Or for short result texts you can use a numeric picture switch to produce the correct result, e.g.

{ =OR({ COMPARE { MERGEFIELD v1 } = 0 },{ COMPARE { MERGEFIELD v2 = 0 }) }) \#"'';'';'Lorem Ipsum..'" }

For more comparands you need to nest the ORs

In this case you could also do stuff such as

{ IF { =ABS({ MERGEFIELD v1 })+ABS({ MERGEFIELD v2 }) } = 0 "Lorem Ipsum.. }

FWIW I do not believe the syntax has changed in Word 2013 in this area, but if you have an example of such a difference, please share!

Solution 2:

The first example above is I think in error. The second looks correct. COMPARE gives 1 if true; 0 if false. =OR works this way, too - arithmetically. So if either of your COMPARE propositions are true, the OR on them will yield 1. You want to output "Lorem Ipsum" if either of the COMPARE propositions are true. Therefore the IF should compare with 1, not with 0. Alternatively, comparing with 0, "Lorem Ipsum" could be output for a FALSE match. I think this is the other error: I think IF expects both true and false arguments to output. {IF <condition> <true-text> <false-text>}.

More complex logical conditions can be expressed economically (without nesting) by again using arithmetic creatively. If, say, it is sufficient for any one of three items to be true, the sum of their truth values can be used as a condition for IF. If all are false, the result will be zero. General arithmetic can be applied in Word fields using {=}. For example:

{IF {=({COMPARE {MERGEFIELD v1} = 0} + {COMPARE {MERGEFIELD v2} = 0} + {COMPARE {MERGEFIELD v3} = 0})} = 0 "None are zero." "At least one is zero."}