multiple IF statements with a mergefield

D

Diane

I'm needing an example of multiple if statements for the mergefield. I
understand this scenario--> {if{ mergefield "data"} = '1' "type this text"
"else type this text"}.


The scenario I have is something like {if{mergefield "data"}="1" "type this
text" else {if {mergefield "data"} = "2" "type text line 2" else if {if
mergefield "data"} "3" "type text line 3". Similar to a CASE statement, but
I'm not quite sure how to do this with MSWORD.

Thanks for any help.
 
E

Ed

Hi Diane,

The basic form of an IF field is:

{ IF some condition is true "Truetext" "FalseText" }

Where "Truetext" is what to insert if the condition is true and "Falsetext"
is what to insert if the condition is false.

To get more complex tests you can nest one IF field inside the Truetext or
the Falsetext of another. (Not sure how many levels of nesting you can have.)

To simulate "AND" you nest into the Truetext and to simulate "OR" you nest
into the Falsetext.

So if I wanted to do something like ...

'IF X = 1 THEN "One" ELSE IF X = 2 THEN "Two"' ...

I would create a field like this ...

{ IF X = 1 "One" "{ IF X = 2 "Two" "X is neither 1 nor 2"}" }

where I've nested an inner IF field inside the Falsetext of the outer one.

If I wanted to check against 3 as well I would just nest another IF field in
place of "X is neither 1 nor 2" ... and so on.

Two suggestions:

It can be easier to create nested IF fields by typing them rather than by
using the menus (use Ctrl-F9 to get the field braces because the curly
bracket keys on the keyboard won't work).

If you are nesting the same type of test over and over, create the
first-level field leaving the relevant Truetext or Falsetext (the one you're
going to paste into) as an empty set of quotation marks and copy the field
(and only it, no trailing spaces or paragraph mark) to the clipboard.

E.g. { IF X = 1 "One" "" }

Then paste into the empty set of quotation marks to get your first level of
nesting.

Then paste again into the (only) empty set of quotation marks to get your
second level of nesting ... and so on.

The when you've got all the levels you want just go over the field and
change "1" to "2", "3" etc. and "One" to "Two", "Three" etc. as appropriate,
and fill in the final Truetext or Falsetext value.

That way you almost guarantee that you get the nesting structure right and
you don't have to worry about the jungle of brackets and quotation marks.

BTW, in your particular case, you probably don't strictly need nested
fields; you could just have one IF field after another on the same line. Only
one of them at most would evaluate to true. Probably less efficient(although
probably not to any degree that matters) and less "elegant" than the nested
method, though.

Regards.

Ed
 
D

Diane

Ed,
Thanks for the examples given. I have it working by following your
examples, I prefer the nested fields, I find it more clear to the reader the
logic of the coding.
(I didn't realize I had already sent my first post, and then repeated the
same question - oops!)

Again, great explanation given.
Diane
 
E

Ed

You're welcome :)

Regards.

Ed

Diane said:
Ed,
Thanks for the examples given. I have it working by following your
examples, I prefer the nested fields, I find it more clear to the reader the
logic of the coding.
(I didn't realize I had already sent my first post, and then repeated the
same question - oops!)

Again, great explanation given.
Diane
 
K

Kim S.

I am trying, without much luck, to create a merge template that will be used to create a membership list. My Excel sheet has the following set up: "GroupType" and "CompanyName" are headers and below each header is "family, donor advised, corporate" matched accordingly to a member name.

This is what I would LIKE to do:

If the GroupType equals Family or donor advised, any CompanyName that is that group type should be merged.

If the GroupType equals Independent, any CompanyName that is that group type should be merged and so on and so on.

Here are some examples of what I attempted to do myself...

{MERGEFIELD CompanyName}{ {IF {MERGEFIELD GroupType} = "Corporate"}}

Any help would be greatly appreciated.

Thanks!!



E wrote:

RE: multiple IF statements with a mergefield
29-Aug-07

You're welcome :)

Regards.

Ed

:

Previous Posts In This Thread:

multiple IF statements with a mergefield
I'm needing an example of multiple if statements for the mergefield. I
understand this scenario--> {if{ mergefield "data"} = '1' "type this text"
"else type this text"}.


The scenario I have is something like {if{mergefield "data"}="1" "type this
text" else {if {mergefield "data"} = "2" "type text line 2" else if {if
mergefield "data"} "3" "type text line 3". Similar to a CASE statement, but
I'm not quite sure how to do this with MSWORD.

Thanks for any help.

RE: multiple IF statements with a mergefield
Hi Diane,

The basic form of an IF field is:

{ IF some condition is true "Truetext" "FalseText" }

Where "Truetext" is what to insert if the condition is true and "Falsetext"
is what to insert if the condition is false.

To get more complex tests you can nest one IF field inside the Truetext or
the Falsetext of another. (Not sure how many levels of nesting you can have.)

To simulate "AND" you nest into the Truetext and to simulate "OR" you nest
into the Falsetext.

So if I wanted to do something like ...

'IF X = 1 THEN "One" ELSE IF X = 2 THEN "Two"' ...

I would create a field like this ...

{ IF X = 1 "One" "{ IF X = 2 "Two" "X is neither 1 nor 2"}" }

where I've nested an inner IF field inside the Falsetext of the outer one.

If I wanted to check against 3 as well I would just nest another IF field in
place of "X is neither 1 nor 2" ... and so on.

Two suggestions:

It can be easier to create nested IF fields by typing them rather than by
using the menus (use Ctrl-F9 to get the field braces because the curly
bracket keys on the keyboard won't work).

If you are nesting the same type of test over and over, create the
first-level field leaving the relevant Truetext or Falsetext (the one you're
going to paste into) as an empty set of quotation marks and copy the field
(and only it, no trailing spaces or paragraph mark) to the clipboard.

E.g. { IF X = 1 "One" "" }

Then paste into the empty set of quotation marks to get your first level of
nesting.

Then paste again into the (only) empty set of quotation marks to get your
second level of nesting ... and so on.

The when you've got all the levels you want just go over the field and
change "1" to "2", "3" etc. and "One" to "Two", "Three" etc. as appropriate,
and fill in the final Truetext or Falsetext value.

That way you almost guarantee that you get the nesting structure right and
you don't have to worry about the jungle of brackets and quotation marks.

BTW, in your particular case, you probably don't strictly need nested
fields; you could just have one IF field after another on the same line. Only
one of them at most would evaluate to true. Probably less efficient(although
probably not to any degree that matters) and less "elegant" than the nested
method, though.

Regards.

Ed

:

Ed, Thanks for the examples given.
Ed,
Thanks for the examples given. I have it working by following your
examples, I prefer the nested fields, I find it more clear to the reader the
logic of the coding.
(I didn't realize I had already sent my first post, and then repeated the
same question - oops!)

Again, great explanation given.
Diane

:

RE: multiple IF statements with a mergefield
You're welcome :)

Regards.

Ed

:


Submitted via EggHeadCafe - Software Developer Portal of Choice
Custom configuration Data from DataSets: Less is More
http://www.eggheadcafe.com/tutorial...d-329a2cdc9f44/custom-configuration-data.aspx
 
D

Doug Robbins - Word MVP

Sorry, your question does not make much sense, but the way you use the {
IF } field is

{ IF [CONDITION] [TRUERESULT] [FALSERESULT] }

It is not clear in your case, what the false result should be and you say
the company name should be inserted for all of the group types that you
mentioned.

If you explain your situation more clearly, we can probably show you how to
go about it.

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 

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