Mail Merge: If ... then ... else

M

Mark Parent

I understand that I can use this function to check a field in my merge data,
and insert one or another text string into my Word document. What I'd like
to do is use a field in my merge data to direct which of two merge fields to
print. Is there a way to implement something like:
If [FIELD1] is Empty then [FIELD2] else [FIELD3] ?
 
P

Peter Jamieson

Yes. It's generally easier to insert this kind of "nested field" using the
keyboard rather than the drop down lists and so on - you need
a. to enable the Mail Merge toolbar (View|Toolbars in WOrd 2002,2003, it
automatically appears for mailmerge documents in earlier versions) Use the
<<>> button to switch between "field view" where you see <<fieldname>> and
"data preview" where you see some result data
b. Alt-F9 to toggle between "field code view" where you see { MERGEFIELD
fieldname } and "field view"/preview. You may find that fields disappear as
you insert them and you need to use Alt-F9 to get them back.
c. select one or more fields and press F9 to update their values
d. ctrl-F9 to insert /each/ pair of the special "field code braces"

The field you need is

{ IF "{ MERGEFIELD field1 }" = "" "{ MERGEFIELD field2 }" "{ MERGEFIELD
field3 }" }

If you don't like all the quotes in this case you can probably get away with

{ IF { MERGEFIELD field1 } = "" { MERGEFIELD field2 } { MERGEFIELD
field3 } }

but I advise you to stick with what I suggested.

If the field name "field1" actually has spaces in it, e.g. is "field 1",
then you need even more quotes to surround the name, e.g.

{ IF "{ MERGEFIELD "field 1" }" = "" "{ MERGEFIELD field2 }" "{ MERGEFIELD
field3 }" }

Peter Jamieson
 
M

Mark Parent

Thanks much.... At the risk of sounding like Chandler Bing, all that Alt-F9,
Ctrl-F9, F9 couldn't BE more complicating, but I'm away to the races now.
Cheers !

Peter Jamieson said:
Yes. It's generally easier to insert this kind of "nested field" using the
keyboard rather than the drop down lists and so on - you need
a. to enable the Mail Merge toolbar (View|Toolbars in WOrd 2002,2003, it
automatically appears for mailmerge documents in earlier versions) Use the
<<>> button to switch between "field view" where you see <<fieldname>> and
"data preview" where you see some result data
b. Alt-F9 to toggle between "field code view" where you see { MERGEFIELD
fieldname } and "field view"/preview. You may find that fields disappear as
you insert them and you need to use Alt-F9 to get them back.
c. select one or more fields and press F9 to update their values
d. ctrl-F9 to insert /each/ pair of the special "field code braces"

The field you need is

{ IF "{ MERGEFIELD field1 }" = "" "{ MERGEFIELD field2 }" "{ MERGEFIELD
field3 }" }

If you don't like all the quotes in this case you can probably get away with

{ IF { MERGEFIELD field1 } = "" { MERGEFIELD field2 } { MERGEFIELD
field3 } }

but I advise you to stick with what I suggested.

If the field name "field1" actually has spaces in it, e.g. is "field 1",
then you need even more quotes to surround the name, e.g.

{ IF "{ MERGEFIELD "field 1" }" = "" "{ MERGEFIELD field2 }" "{ MERGEFIELD
field3 }" }

Peter Jamieson

Mark Parent said:
I understand that I can use this function to check a field in my merge
data,
and insert one or another text string into my Word document. What I'd
like
to do is use a field in my merge data to direct which of two merge fields
to
print. Is there a way to implement something like:
If [FIELD1] is Empty then [FIELD2] else [FIELD3] ?
 
G

Graham Mayor

The three listed commands that control fields quickly become second nature
when working with merge documents.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Mark said:
Thanks much.... At the risk of sounding like Chandler Bing, all that
Alt-F9, Ctrl-F9, F9 couldn't BE more complicating, but I'm away to
the races now. Cheers !

Peter Jamieson said:
Yes. It's generally easier to insert this kind of "nested field"
using the keyboard rather than the drop down lists and so on - you
need a. to enable the Mail Merge toolbar (View|Toolbars in WOrd
2002,2003, it automatically appears for mailmerge documents in
earlier versions) Use the <<>> button to switch between "field view"
where you see <<fieldname>> and "data preview" where you see some
result data b. Alt-F9 to toggle between "field code view" where you
see { MERGEFIELD fieldname } and "field view"/preview. You may find
that fields disappear as you insert them and you need to use Alt-F9
to get them back. c. select one or more fields and press F9 to
update their values d. ctrl-F9 to insert /each/ pair of the special
"field code braces"

The field you need is

{ IF "{ MERGEFIELD field1 }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

If you don't like all the quotes in this case you can probably get
away with

{ IF { MERGEFIELD field1 } = "" { MERGEFIELD field2 } { MERGEFIELD
field3 } }

but I advise you to stick with what I suggested.

If the field name "field1" actually has spaces in it, e.g. is "field
1", then you need even more quotes to surround the name, e.g.

{ IF "{ MERGEFIELD "field 1" }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

Peter Jamieson

Mark Parent said:
I understand that I can use this function to check a field in my
merge data,
and insert one or another text string into my Word document. What
I'd like
to do is use a field in my merge data to direct which of two merge
fields to
print. Is there a way to implement something like:
If [FIELD1] is Empty then [FIELD2] else [FIELD3] ?
 
C

Curt

Have used if then else in excel. Not familar good with word. Want to have
word stop printing when it finds a blank cell. This is in a mail merge macro.
Is there code to do this? I have written procedures in excel. No expert self
tought. Having a good time. Those of you that man this group are lifesavers.
Thanks

Graham Mayor said:
The three listed commands that control fields quickly become second nature
when working with merge documents.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Mark said:
Thanks much.... At the risk of sounding like Chandler Bing, all that
Alt-F9, Ctrl-F9, F9 couldn't BE more complicating, but I'm away to
the races now. Cheers !

Peter Jamieson said:
Yes. It's generally easier to insert this kind of "nested field"
using the keyboard rather than the drop down lists and so on - you
need a. to enable the Mail Merge toolbar (View|Toolbars in WOrd
2002,2003, it automatically appears for mailmerge documents in
earlier versions) Use the <<>> button to switch between "field view"
where you see <<fieldname>> and "data preview" where you see some
result data b. Alt-F9 to toggle between "field code view" where you
see { MERGEFIELD fieldname } and "field view"/preview. You may find
that fields disappear as you insert them and you need to use Alt-F9
to get them back. c. select one or more fields and press F9 to
update their values d. ctrl-F9 to insert /each/ pair of the special
"field code braces"

The field you need is

{ IF "{ MERGEFIELD field1 }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

If you don't like all the quotes in this case you can probably get
away with

{ IF { MERGEFIELD field1 } = "" { MERGEFIELD field2 } { MERGEFIELD
field3 } }

but I advise you to stick with what I suggested.

If the field name "field1" actually has spaces in it, e.g. is "field
1", then you need even more quotes to surround the name, e.g.

{ IF "{ MERGEFIELD "field 1" }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

Peter Jamieson

I understand that I can use this function to check a field in my
merge data,
and insert one or another text string into my Word document. What
I'd like
to do is use a field in my merge data to direct which of two merge
fields to
print. Is there a way to implement something like:
If [FIELD1] is Empty then [FIELD2] else [FIELD3] ?
 
P

Peter Jamieson

Your other messages suggest that you may have solved this problem, but if
not...

1. I don't think a Word { IF } field will help here, because it doesn't let
you terminate a merge (i.e. you can't do something like

IF condition THEN
exit function
ELSE
continue merging
END IF

If your Excel macro is starting Word, then performing a MailMerge.Execute,
it's already "too late" - the start and end records have been selected and
the merge will merge those records, /unless/ you are also using Merge Events
in Word, in which case you can cancel the merge from within.

However, since your merge is running from Excel, I would suggest that it
would be simpler to run some Excel code to find the first blank cell in the
column that you want to test, (say it is in row N of the sheet) then set the
merge so that it merges rows 2 to N-1

Peter Jamieson

Curt said:
Have used if then else in excel. Not familar good with word. Want to have
word stop printing when it finds a blank cell. This is in a mail merge
macro.
Is there code to do this? I have written procedures in excel. No expert
self
tought. Having a good time. Those of you that man this group are
lifesavers.
Thanks

Graham Mayor said:
The three listed commands that control fields quickly become second
nature
when working with merge documents.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Mark said:
Thanks much.... At the risk of sounding like Chandler Bing, all that
Alt-F9, Ctrl-F9, F9 couldn't BE more complicating, but I'm away to
the races now. Cheers !

:

Yes. It's generally easier to insert this kind of "nested field"
using the keyboard rather than the drop down lists and so on - you
need a. to enable the Mail Merge toolbar (View|Toolbars in WOrd
2002,2003, it automatically appears for mailmerge documents in
earlier versions) Use the <<>> button to switch between "field view"
where you see <<fieldname>> and "data preview" where you see some
result data b. Alt-F9 to toggle between "field code view" where you
see { MERGEFIELD fieldname } and "field view"/preview. You may find
that fields disappear as you insert them and you need to use Alt-F9
to get them back. c. select one or more fields and press F9 to
update their values d. ctrl-F9 to insert /each/ pair of the special
"field code braces"

The field you need is

{ IF "{ MERGEFIELD field1 }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

If you don't like all the quotes in this case you can probably get
away with

{ IF { MERGEFIELD field1 } = "" { MERGEFIELD field2 } { MERGEFIELD
field3 } }

but I advise you to stick with what I suggested.

If the field name "field1" actually has spaces in it, e.g. is "field
1", then you need even more quotes to surround the name, e.g.

{ IF "{ MERGEFIELD "field 1" }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

Peter Jamieson

I understand that I can use this function to check a field in my
merge data,
and insert one or another text string into my Word document. What
I'd like
to do is use a field in my merge data to direct which of two merge
fields to
print. Is there a way to implement something like:
If [FIELD1] is Empty then [FIELD2] else [FIELD3] ?
 
C

Curt

Thank You for explination Helps a novice like me a lot
Thanks

Peter Jamieson said:
Your other messages suggest that you may have solved this problem, but if
not...

1. I don't think a Word { IF } field will help here, because it doesn't let
you terminate a merge (i.e. you can't do something like

IF condition THEN
exit function
ELSE
continue merging
END IF

If your Excel macro is starting Word, then performing a MailMerge.Execute,
it's already "too late" - the start and end records have been selected and
the merge will merge those records, /unless/ you are also using Merge Events
in Word, in which case you can cancel the merge from within.

However, since your merge is running from Excel, I would suggest that it
would be simpler to run some Excel code to find the first blank cell in the
column that you want to test, (say it is in row N of the sheet) then set the
merge so that it merges rows 2 to N-1

Peter Jamieson

Curt said:
Have used if then else in excel. Not familar good with word. Want to have
word stop printing when it finds a blank cell. This is in a mail merge
macro.
Is there code to do this? I have written procedures in excel. No expert
self
tought. Having a good time. Those of you that man this group are
lifesavers.
Thanks

Graham Mayor said:
The three listed commands that control fields quickly become second
nature
when working with merge documents.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Mark Parent wrote:
Thanks much.... At the risk of sounding like Chandler Bing, all that
Alt-F9, Ctrl-F9, F9 couldn't BE more complicating, but I'm away to
the races now. Cheers !

:

Yes. It's generally easier to insert this kind of "nested field"
using the keyboard rather than the drop down lists and so on - you
need a. to enable the Mail Merge toolbar (View|Toolbars in WOrd
2002,2003, it automatically appears for mailmerge documents in
earlier versions) Use the <<>> button to switch between "field view"
where you see <<fieldname>> and "data preview" where you see some
result data b. Alt-F9 to toggle between "field code view" where you
see { MERGEFIELD fieldname } and "field view"/preview. You may find
that fields disappear as you insert them and you need to use Alt-F9
to get them back. c. select one or more fields and press F9 to
update their values d. ctrl-F9 to insert /each/ pair of the special
"field code braces"

The field you need is

{ IF "{ MERGEFIELD field1 }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

If you don't like all the quotes in this case you can probably get
away with

{ IF { MERGEFIELD field1 } = "" { MERGEFIELD field2 } { MERGEFIELD
field3 } }

but I advise you to stick with what I suggested.

If the field name "field1" actually has spaces in it, e.g. is "field
1", then you need even more quotes to surround the name, e.g.

{ IF "{ MERGEFIELD "field 1" }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

Peter Jamieson

I understand that I can use this function to check a field in my
merge data,
and insert one or another text string into my Word document. What
I'd like
to do is use a field in my merge data to direct which of two merge
fields to
print. Is there a way to implement something like:
If [FIELD1] is Empty then [FIELD2] else [FIELD3] ?
 
E

elg

Hi,

I am totaly new at this complexity of merge and want to be sure I understand
where these logic strings should go--you place them right in the middle of
your sequence of fields to build an address. I was trying to follow
instructions from word mvp to build a complex address where not every filed
in every record would have a value and came up with this result:
{ MERGEFIELD jobtitle }{ IF { MERGEFIELD jobtitle } = "" "" " " }

{ MERGEFIELD org1 }{ IF { MERGEFIELD org1 } = "" "" " " }

{ MERGEFIELD org2 }{ IF { MERGEFIELD org2 } = "" "" " " }

{ MERGEFIELD jobtitle }{ IF { MERGEFIELD jobtitle } = "" "" " " }

{ MERGEFIELD address2 }{ IF { MERGEFIELD address2 } = "" "" " " }


but when I ran the merge, only the name field got filled in. I would
greatly appreciate any help you can offer.


Curt said:
Thank You for explination Helps a novice like me a lot
Thanks

Peter Jamieson said:
Your other messages suggest that you may have solved this problem, but if
not...

1. I don't think a Word { IF } field will help here, because it doesn't let
you terminate a merge (i.e. you can't do something like

IF condition THEN
exit function
ELSE
continue merging
END IF

If your Excel macro is starting Word, then performing a MailMerge.Execute,
it's already "too late" - the start and end records have been selected and
the merge will merge those records, /unless/ you are also using Merge Events
in Word, in which case you can cancel the merge from within.

However, since your merge is running from Excel, I would suggest that it
would be simpler to run some Excel code to find the first blank cell in the
column that you want to test, (say it is in row N of the sheet) then set the
merge so that it merges rows 2 to N-1

Peter Jamieson

Curt said:
Have used if then else in excel. Not familar good with word. Want to have
word stop printing when it finds a blank cell. This is in a mail merge
macro.
Is there code to do this? I have written procedures in excel. No expert
self
tought. Having a good time. Those of you that man this group are
lifesavers.
Thanks

:

The three listed commands that control fields quickly become second
nature
when working with merge documents.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Mark Parent wrote:
Thanks much.... At the risk of sounding like Chandler Bing, all that
Alt-F9, Ctrl-F9, F9 couldn't BE more complicating, but I'm away to
the races now. Cheers !

:

Yes. It's generally easier to insert this kind of "nested field"
using the keyboard rather than the drop down lists and so on - you
need a. to enable the Mail Merge toolbar (View|Toolbars in WOrd
2002,2003, it automatically appears for mailmerge documents in
earlier versions) Use the <<>> button to switch between "field view"
where you see <<fieldname>> and "data preview" where you see some
result data b. Alt-F9 to toggle between "field code view" where you
see { MERGEFIELD fieldname } and "field view"/preview. You may find
that fields disappear as you insert them and you need to use Alt-F9
to get them back. c. select one or more fields and press F9 to
update their values d. ctrl-F9 to insert /each/ pair of the special
"field code braces"

The field you need is

{ IF "{ MERGEFIELD field1 }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

If you don't like all the quotes in this case you can probably get
away with

{ IF { MERGEFIELD field1 } = "" { MERGEFIELD field2 } { MERGEFIELD
field3 } }

but I advise you to stick with what I suggested.

If the field name "field1" actually has spaces in it, e.g. is "field
1", then you need even more quotes to surround the name, e.g.

{ IF "{ MERGEFIELD "field 1" }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

Peter Jamieson

I understand that I can use this function to check a field in my
merge data,
and insert one or another text string into my Word document. What
I'd like
to do is use a field in my merge data to direct which of two merge
fields to
print. Is there a way to implement something like:
If [FIELD1] is Empty then [FIELD2] else [FIELD3] ?
 
G

Graham Mayor

It is difficult to see the relevance of your particular field names, but if
I show a typical example of conditional fields in an address, maybe you can
work it out from there?

{ Mergefield FirstName } { Mergefield LastName }¶
{ IF{ Mergefield JobTitle } <> "" "{ Mergefield JobTitle }¶
" }{ IF{ Mergefield Company } <> "" "{ Mergefield Company }¶
" }{ Mergefield Address1 }¶
{ IF { Mergefield Address2 } <> "" "{ Mergefield Address2 }¶
" }{Mergefield Address 3 } etc

Press Enter where you see ¶ and nowhere else.

Your example
{ MERGEFIELD org1 }{ IF { MERGEFIELD org1 } = "" "" " " }
enters the content of org1 then if Org1 is not empty it enters a space. This
can be written more simply as
{ MERGEFIELD org1 }{ IF { MERGEFIELD org1 } <> "" " " }
or
{ IF { MERGEFIELD org1 } <> "" "{ MERGEFIELD org1 } " }
ie If Org1 has content (not empty) write org1 and a space.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Hi,

I am totaly new at this complexity of merge and want to be sure I
understand where these logic strings should go--you place them right
in the middle of your sequence of fields to build an address. I was
trying to follow instructions from word mvp to build a complex
address where not every filed in every record would have a value and
came up with this result: { MERGEFIELD jobtitle }{ IF { MERGEFIELD
jobtitle } = "" "" " " }

{ MERGEFIELD org1 }{ IF { MERGEFIELD org1 } = "" "" " " }

{ MERGEFIELD org2 }{ IF { MERGEFIELD org2 } = "" "" " " }

{ MERGEFIELD jobtitle }{ IF { MERGEFIELD jobtitle } = "" "" " " }

{ MERGEFIELD address2 }{ IF { MERGEFIELD address2 } = "" "" " " }


but when I ran the merge, only the name field got filled in. I would
greatly appreciate any help you can offer.


Curt said:
Thank You for explination Helps a novice like me a lot
Thanks

Peter Jamieson said:
Your other messages suggest that you may have solved this problem,
but if not...

1. I don't think a Word { IF } field will help here, because it
doesn't let you terminate a merge (i.e. you can't do something like

IF condition THEN
exit function
ELSE
continue merging
END IF

If your Excel macro is starting Word, then performing a
MailMerge.Execute, it's already "too late" - the start and end
records have been selected and the merge will merge those records,
/unless/ you are also using Merge Events in Word, in which case you
can cancel the merge from within.

However, since your merge is running from Excel, I would suggest
that it would be simpler to run some Excel code to find the first
blank cell in the column that you want to test, (say it is in row N
of the sheet) then set the merge so that it merges rows 2 to N-1

Peter Jamieson

Have used if then else in excel. Not familar good with word. Want
to have word stop printing when it finds a blank cell. This is in
a mail merge macro.
Is there code to do this? I have written procedures in excel. No
expert self
tought. Having a good time. Those of you that man this group are
lifesavers.
Thanks

:

The three listed commands that control fields quickly become
second nature
when working with merge documents.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Mark Parent wrote:
Thanks much.... At the risk of sounding like Chandler Bing, all
that Alt-F9, Ctrl-F9, F9 couldn't BE more complicating, but I'm
away to the races now. Cheers !

:

Yes. It's generally easier to insert this kind of "nested field"
using the keyboard rather than the drop down lists and so on -
you need a. to enable the Mail Merge toolbar (View|Toolbars in
WOrd 2002,2003, it automatically appears for mailmerge
documents in earlier versions) Use the <<>> button to switch
between "field view" where you see <<fieldname>> and "data
preview" where you see some result data b. Alt-F9 to toggle
between "field code view" where you see { MERGEFIELD fieldname
} and "field view"/preview. You may find that fields disappear
as you insert them and you need to use Alt-F9 to get them
back. c. select one or more fields and press F9 to update
their values d. ctrl-F9 to insert /each/ pair of the special
"field code braces"

The field you need is

{ IF "{ MERGEFIELD field1 }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

If you don't like all the quotes in this case you can probably
get away with

{ IF { MERGEFIELD field1 } = "" { MERGEFIELD field2 } {
MERGEFIELD field3 } }

but I advise you to stick with what I suggested.

If the field name "field1" actually has spaces in it, e.g. is
"field 1", then you need even more quotes to surround the name,
e.g.

{ IF "{ MERGEFIELD "field 1" }" = "" "{ MERGEFIELD field2 }" "{
MERGEFIELD field3 }" }

Peter Jamieson

message
I understand that I can use this function to check a field in
my merge data,
and insert one or another text string into my Word document.
What I'd like
to do is use a field in my merge data to direct which of two
merge fields to
print. Is there a way to implement something like:
If [FIELD1] is Empty then [FIELD2] else [FIELD3] ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top