tricky IF merge criteria

H

hbear

Is it possible to use the last character of the value in one field as the
criteria in an IF merge statement? To clarify, it would be something like
this: IF <last character in "FieldX" is "1", Then "A", Else "B">. If this is
possible, I can think of several uses I could put it to in current and future
projects, and I'd love to see what the experts here have to say.

Thanks!
 
P

Peter Jamieson

You should be able to do

{ IF { MERGEFIELD FieldX } = "*1" "A" "B" }

(assuming FieldX is a merge field) but
a. only if the text in MergeX isn't above 128 characters
b. just so you know, you can also use "?" to wildcard a single character
but you can't do a "contains" by using

{ IF { MERGEFIELD FieldX } = "*1*" "A" "B" }

If you're using aa SET field, things may get stuck if the value exceeds 128
characters - i.e. if you do

{ SET X "128 characters.... followed by a 1" }
{ X }
{ IF "{ X }" = "*1" "A" "B" }

Then you get a message "Error! Trailing portion of string too long for
wildcard match."
Then even if you change the SET to

{ SET X "1" } and update all the fields, you may still see that error. { X}
evaluates correctly outside the IF, but if you select the one inside the IF,
right-click and toggle it to see its value, it seems to be stuck in the
state that caused the error. Delete and re-insert { X } and you're OK again.
But maybe you don't need to know that, or it's some quirk on my system. This
is another case where { IF x = "*1" "A" "B" } works better than the { IF
"{ X }" = "*1" "A" "B" } that I normally prefer.

Peter Jamieson
 
H

hbear

*1! I could kick myself for missing that one. But there's tons of great
information here. Thanks so much for sharing!
 
H

hbear

Peter, This worked wonderfully. Until... I ran into a field with more than
128 characters. Then I got the "Error! Trailing portion..." issue you
mention. I was wondering if it would be possible to test the length of the
field before running the criteria test. My original thought was something
along the lines {IF len{MergefieldX}>128 "" "(run criteria test here)"}. I
tried that to no avail, then looked in the field list and chanced across the
NUMCHAR code. The description says it displays the number of characters in
the document, but I wonder if it can be used to test the number of characters
in a field. I've played with it a bit but no luck yet. Any thoughts or
suggestions on this?

Thanks.
 
P

Peter Jamieson

That's unfortunate.

No, there's no len() function within the "field language" and NUMCHARS is
just for the total length of the document.

In this case your best bet is to change or process the data source in some
way.

For example, you may be able to use SQL to do the test, but whter you can do
that and how to do it depend on
a. what the data source is (and what you are in a position to do with it)
b. what version of Word

e.g. if the data source is an Access table or query or SQL Server table or
query, you can probably write a a query/view that does the test or selects
the final character of the field into a new column. For example, in Access
you might use right(X,1) to get the last character.

Even if the data source isn't obviously a "database" you may stil be able to
use this kind of SQL by issuing it from Word VBA. However, it is also
possible that you will run into similar restrictions on the lengths of
comparands.

Peter Jamieson
 
H

hbear

The source is an Access database and your suggestion for dealing with the
problem from there worked perfectly. I created a calculated field in the
source query and the merge ran like a dream.
Thanks so much.

all the best!
hb
 

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