MS Word field functions: OR function

At work I am using hybrid of Interop and OpenXml to create some reports on client side. These reports are created from MS Word templates. Because MS Word is our preferred text editor I also can use MS Word functions for some operations. And today I was having fun using function OR. MS Word functions are very simple to use, but if you make one itsy bitsy tiny mistake in syntax some generic error appears which tells you less than nothing (something like “!Syntax Error ,,“).

I needed to check inside of IF field if document property has equal value as one of two other values and then do something. In pseudo code it would look something like this:

if(value is equal value1 OR value is equal value2)
  then do something
  do something else

The thing is that OR function works perfectly with numbers, but not with strings. With numbers it looks like:

{ =OR(1 = 1; 1 = 5) }

This would return 1, as expected (warning: you must use semicolon , not comma). But with strings it would return syntax error. With strings you need to use field COMPARE:

{ =OR( { COMPARE "value" = "value1" }; { COMPARE "value" = "value2" }) }

COMPARE returns 1 if condition is met or returns 0 if condition is not met. If one of COMPAREs returns 1, condition inside OR is met – OR returns 1. In other case it returns 0. IF field would look like this:

{ IF { =OR( { COMPARE "value" = "value1" }; { COMPARE "value" = "value2" })} = 1 "when TRUE, use this" "when FALSE, use this"}

As I said, very simple to use, but when you have a very long sausage of text with IF fields and nested functions every small mistake becomes great pain in the arse (pardon my language).

Leave a Comment.