Hi,
My goal is to mail merge from an excel file on to labels in word. I
have an excel file with First Name, Last Name, Spouse First and
Spouse Last. I need code that will create lables for three
scenarios:
1. Sally and John Doe
2. Adam Jones and Mary Smith
3. Bob Price
a simple solution would be to add a column STATUS to the Excel data
source. In this column you could insert "0" by using IF function, if
there is no spouse (Spouse First = empty), "1" if Spouse Last = Last
Name, "2" if Spouse Last <> Last Name.
So in the Word main document you can use the rule IF ... THEN ... ELSE:
If STATUS = 0 then Mergefield First Name, Mergefield Last Name,
otherwise
If STATUS = 1 then Mergefield First Name, and, Mergefield Spouse First,
Mergefield Last Name,
otherwise
Mergefield First Name, Mergefield Last Name, and, Mergefield Spouse
First, Mergefield Spouse Last.
If you don't want to use an addional Excel column, of course, you can
nest multiple conditions in Word, as shown in Graham's code.
The pair of curly brackets "{ }" always have to be inserted by pressing
Ctrl+F9. But I find it easier to use the "If ... then ... else" mail
merge rule, to make sure the correct syntax (brackets, spaces, quotes).
To nest multiple comparisons you can complete the first comparison, and
then set an asterisk where you cannot continue.
Afterwards, in the main document show the field codes by pressing Alt +
F9. Select the asterisk and call the "If ... then ... else" rule again.
This second rule will overwrite your asterisk, and so on.
Finally your code will be similar to Graham's code:
{ IF { Mergefield Spouse Last } <> ""
"{ IF{ Mergefield Last Name } <> { Mergefield Spouse Last }
"{ Mergefield First Name } { Mergefield Last Name } &
{ Mergefield Spouse First} { Mergefield Spouse Last }"
"{ IF Mergefield Last Name } = { Mergefield Spouse Last}
"{ Mergefield First Name } & { Mergefield Spouse First} { Mergefield
Last Name }"
"{ Mergefield First Name } { Mergefield Last Name }"}
Maybe, there are missing quotes and/or brackets. However, when using
the "If ... then ... else" rule, Word will insert those elements.