ms-worddocx-mailmergefieldcodes

Word mail merge errors in "by category"


Please see the attachments for the mail merge issue. The result expected by me should have been:

enter image description here

But it turns out to be:

enter image description here

I believe the error is caused by the comparison in the field codes (i.e. <>) but I couldn't figure out where the error is. If I make a change to the data,

1A --> F1A OR 1A --> A1

The error disappears. Here are the field codes:

enter image description here

Thanks for any help in advance!


Solution

  • You need to put double quotation marks around { Place2 } and { Place1 }, e.g.

    "{ Place2 }"
    

    As it is, when word does the comparison, it will evaluate values such as 1A, 2D etc. as the numbers 1 , 2 etc. Since your list starts with 1A, 1C, the comparison will be { IF 1 <> 1 } so the transition from 1A to 1C will be missed.

    If you want to ensure that Word does a textual comparison, this is one reason. In fact in this scenario Word treats things that look like simple arithmetic expressions as such so if Place1 was called "2*4" and Place2 was called "8", you would get a match if you did not include the quotation marks.

    There are other reasons why it is advisable to quote the comparands in an IF field when you want them to be treated as text. For example if you have the following, X is definitely "abc" and Y is definitely "def" .

    { SET X "abc" }{ SET abc "def" }{ SET Y "def" }{ X }{ Y }
    

    But this will return "equal"

    { IF { X } = { Y } "equal" "not equal" }
    

    whereas this will return "not equal"

    { IF "{ X }" = "{ Y }" "equal" "not equal" }
    

    In other words, if a comparand is not quoted and evaluates to the name of a bookmark in the document, it is treated as a reference to the bookmark value and is dereferenced.