Share via

Word 2010 Mail Merge IF Comparison Between Fields

Anonymous
2014-04-30T12:32:17+00:00

I wonder if anyone can help with the following please.

I want to compare two Mail Merge Fields within Word 2010, and depending on whether or not they match return a different string in my document. This works fine unless both fields being compared begin with the same number.

Just to demonstrate my datasource is an Excel 2010 XLSX file with two columns. Both columns are formatted as Text.

In Word I've linked to the datasource and I am using the following IF command

{ IF { MERGEFIELD FIELD1 } = { MERGEFIELD FIELD2 } "SAME" "DIFFERENT" }

When both fields begin with the same number this will always return 'SAME'. If FIELD2 starts with a different number or contains no numbers then it will correctly return 'DIFFERENT'. The following table shows some example results.

FIELD1 FIELD2 IF COMPARISON RESULT
1 ADDRESS 1 ADDRESS TWO SAME
1ADDRESS 1 ADDR SAME
1 ADDRESS 1 ADDR2 TESTING SAME
1 ADDRESS 2 ADDR DIFFERENT
ADDRESS ADDRESS TWO DIFFERENT

I suspect that it may be Word is not comparing these fields as text when they begin with a number but, if this is the case, I'm not sure how to override this?

Any advice would be greatly appreciated.

Thanks, Andrew

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-04-30T17:28:46+00:00

If you want to ensure a text comparison, you should put at least the first comparand inside quotation marks, e.g.

{ IF "{ MERGEFIELD FIELD1 }" = { MERGEFIELD FIELD2 } "SAME" "DIFFERENT" }

If you do not do that, there can actually be two problems

 a. Word treats the fields as numeric (as in your table)

 b. If the first comparand - { MERGEFIELD FIELD1 } - resolves to a text that is the name of a bookmark, the value of the bookmark will be compared with the value of the second comparand.

the one where the text in the field is the name of a bookmark, in which case the value of the bookmark may be used in the comparison.

So if you have, e.g.

{ SET abc "xxx" }

and { MERGEFIELD FIELD1 } is "abc" and { MERGEFIELD FIELD2 } is either "abc" or "xxx", the result of 

{ IF { MERGEFIELD FIELD1 } = { MERGEFIELD FIELD2 } "SAME" "DIFFERENT" }

will be "SAME"

but in the case where { MERGEFIELD FIELD2 } is "xxx", the result of 

{ IF "{ MERGEFIELD FIELD1 }" = { MERGEFIELD FIELD2 } "SAME" "DIFFERENT" }

will be "DIFFERENT".

Although I think that is strictly enough to avoid those problems, I prefer the clarity of quoting the other thing I expect to be treated as a string as well:

{ IF "{ MERGEFIELD FIELD1 }" = "{ MERGEFIELD FIELD2 }" "SAME" "DIFFERENT" }

Was this answer helpful?

4 people found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-05-01T08:09:07+00:00

    Thank you Peter, putting the quotes around the field worked perfectly.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-04-30T13:23:23+00:00

    I suspect that it may be Word is not comparing these fields as text when they begin with a number but, if this is the case, I'm not sure how to override this?

     

    Typically after posting I've found a solution/work around. If I prefix a letter to the field when doing the comparison it appears to force Word to compare the fields as text and give the result I'm expecting.

    { IF { MERGEFIELD FIELD1 \b Z } = { MERGEFIELD FIELD2 \b Z } "SAME" "DIFFERENT" }

    I would still be interested to know if there is a 'proper' way of doing this?

    Was this answer helpful?

    0 comments No comments