Home > front end >  Else value in nested IF mergefield not working in Word
Else value in nested IF mergefield not working in Word

Time:08-13

I'm trying to get a nested IF statement to work but the ELSE statement doesn't seem to be resolving. Everything else seems to behaving correctly including the various outputs. What am I missing in my statement?

{ IF { MERGEFIELD CODE } = 0001 "Case 1" 
{ IF { MERGEFIELD CODE } = 0002 "Case 2" 
{ IF { MERGEFIELD CODE } = 0003 "Case 3"
{ IF { MERGEFIELD CODE } = 0004 "Case 4"
{ IF { MERGEFIELD CODE } = 0005 "Case 5"
{ IF { MERGEFIELD CODE } = 0006 "Case 6"
{ IF { MERGEFIELD CODE } = 0007 "Case 7" 
}}}}}}
"{ MERGEFIELD ELSEOUTPUT }"
}

CodePudding user response:

If your outputs positive numbers if valid and 0 or nothing otherwise, you could use just:

{MERGEFIELD CODE \# "'Case '0;;'{MERGEFIELD ELSEOUTPUT}'"}

Otherwise, you might use:

{IF{MERGEFIELD CODE \# 0000}= "0001" "Case 1" 
{IF{MERGEFIELD CODE \# 0000}= "0002" "Case 2" 
{IF{MERGEFIELD CODE \# 0000}= "0003" "Case 3" 
{IF{MERGEFIELD CODE \# 0000}= "0004" "Case 4" 
{IF{MERGEFIELD CODE \# 0000}= "0005" "Case 5" 
{IF{MERGEFIELD CODE \# 0000}= "0006" "Case 6" 
{IF{MERGEFIELD CODE \# 0000}= "0007" "Case 7" {MERGEFIELD ELSEOUTPUT}}}}}}}}

or:

{IF{MERGEFIELD CODE}= 1 "Case 1" 
{IF{MERGEFIELD CODE}= 2 "Case 2" 
{IF{MERGEFIELD CODE}= 3 "Case 3" 
{IF{MERGEFIELD CODE}= 4 "Case 4" 
{IF{MERGEFIELD CODE}= 5 "Case 5" 
{IF{MERGEFIELD CODE}= 6 "Case 6" 
{IF{MERGEFIELD CODE}= 7 "Case 7" {MERGEFIELD ELSEOUTPUT}}}}}}}}

CodePudding user response:

The main thing wrong is the nesting - you need to relocate your

"{ MERGEFIELD ELSEOUTPUT }"

so that you have

"Case 7" "{ MERGEFIELD ELEEOUTPUT } }

The explanation is as follows.

The syntax for an IF field is (simplified)

{ IF condition true-result false-result }

Anything after false-result will just be ignored. Try, e.g.

{ IF 1 = 1 "A" "B" "C" }

(You should see A )

and

{ IF 1 = 2 "A" "B" "C" }

(You should see B )

The "C" value will never be in the result.

Simplifying your IF statement down to a couple of cases, you have something like

{ IF X = 1 "1" { IF X = 2 "2" } { ELSEOUTPUT } }

So the "1" is the equivalent of the "A", the { IF X = 2 "2" } the equivalent of the "B" and the { ELSEOUTPUT } is the equivalent of the "C"

i.e. the { ELSEOUTPUT } will never be in the result.

So you need to move your { MERGEFIELD ELSEOUTPUT } so that it is the false-result of the 0007 test, i.e.perhaps

{ IF { MERGEFIELD CODE } = 0001 "Case 1" 
{ IF { MERGEFIELD CODE } = 0002 "Case 2" 
{ IF { MERGEFIELD CODE } = 0003 "Case 3"
{ IF { MERGEFIELD CODE } = 0004 "Case 4"
{ IF { MERGEFIELD CODE } = 0005 "Case 5"
{ IF { MERGEFIELD CODE } = 0006 "Case 6"
{ IF { MERGEFIELD CODE } = 0007 "Case 7" "{ MERGEFIELD ELSEOUTPUT }"
}}}}}}}

or to follow your existing layout, something like

{ IF { MERGEFIELD CODE } = 0001 "Case 1" 
{ IF { MERGEFIELD CODE } = 0002 "Case 2" 
{ IF { MERGEFIELD CODE } = 0003 "Case 3"
{ IF { MERGEFIELD CODE } = 0004 "Case 4"
{ IF { MERGEFIELD CODE } = 0005 "Case 5"
{ IF { MERGEFIELD CODE } = 0006 "Case 6"
{ IF { MERGEFIELD CODE } = 0007 "Case 7" 
  "{ MERGEFIELD ELSEOUTPUT }"
}}}}}}}

Beyond that, whether the IF will work exactly as you intend depends on what values the CODE field can have and how you want each value to be treated.

As long as your codes are all 4-digit numeric codes, or they are numeric codes and you want "1", "01" etc. to be treated the same way as "0001" then the IF field should work as it is. Or you could simplify it in various ways.

In the rather less common situation where you need "1" to be treated differently from "0001", you would need to quote the { MERGEFIELD CODE } field, e.g. assuming "1" needs to result in ELSEOUTPUT you need

{ IF "{ MERGEFIELD CODE }" = 0001 "Case 1" 
{ IF "{ MERGEFIELD CODE }" = 0002 "Case 2" 
{ IF "{ MERGEFIELD CODE }" = 0003 "Case 3"
{ IF "{ MERGEFIELD CODE }" = 0004 "Case 4"
{ IF "{ MERGEFIELD CODE }" = 0005 "Case 5"
{ IF "{ MERGEFIELD CODE }" = 0006 "Case 6"
{ IF "{ MERGEFIELD CODE }" = 0007 "Case 7" 
  "{ MERGEFIELD ELSEOUTPUT }"
}}}}}}}

Although to make everything as clear as possible I would favour

{ IF "{ MERGEFIELD CODE }" = "0001" "Case 1" 
{ IF "{ MERGEFIELD CODE }" = "0002" "Case 2" 
{ IF "{ MERGEFIELD CODE }" = "0003" "Case 3"
{ IF "{ MERGEFIELD CODE }" = "0004" "Case 4"
{ IF "{ MERGEFIELD CODE }" = "0005" "Case 5"
{ IF "{ MERGEFIELD CODE }" = "0006" "Case 6"
{ IF "{ MERGEFIELD CODE }" = "0007" "Case 7" 
  "{ MERGEFIELD ELSEOUTPUT }"
}}}}}}}

Quoting the "0001" and not quoting the "{ MERGEFIELD CODE }" means that the comparison is still numeric and won't work as intended.

That will also deal with two other "edge" cases, i.e.

  • If your codes can be alphanumeric, if you do not quote the { MERGEFIELD CODE } field, codes like "3ABC", "03AB" and even "01 2" will also match with 0003.

  • If { MERGEFIELD CODE } resolves to the name of a bookmark in your mail merge main document and you do not quote the field or the other comparand, the IF field will actually compare the value of that bookmark. Yes, it's a really weird quirk of the IF field, but it's why I favour quoting anything that you want to be treated as alphanumeric.

So e.g. if { MERGEFIELD CODE } has value "ABCD" and somewhere before your { IF } field you have { SET ABCD 4 } if you have

{ IF { MERGEFIELD CODE } = 0004 "Case 4" "{ MERGEFIELD ELSEOUTPUT }" }

then the result will be Case 4, not the ELSEOUTPUT value.

  • Related