Group:  Microsoft Word ยป microsoft.public.word.mailmerge.fields
Thread: tricky IF merge criteria

Geek News

tricky IF merge criteria
hbear 6/28/2007 3:26:03 PM
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!

Re: tricky IF merge criteria
"Peter Jamieson" <pjj[ at ]KillmapSpjjnet.demon.co.uk> 6/28/2007 4:46:24 PM
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

"hbear" <hbear[ at ]discussions.microsoft.com> wrote in message
news:3C1FF5B8-260B-462F-8A4C-4C2CB12273F7[ at ]microsoft.com...
[Quoted Text]
> 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!
>

Re: tricky IF merge criteria
hbear 6/28/2007 6:34:01 PM
*1! I could kick myself for missing that one. But there's tons of great
information here. Thanks so much for sharing!

"Peter Jamieson" wrote:

[Quoted Text]
> 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
>
> "hbear" <hbear[ at ]discussions.microsoft.com> wrote in message
> news:3C1FF5B8-260B-462F-8A4C-4C2CB12273F7[ at ]microsoft.com...
> > 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!
> >
>
>
Re: tricky IF merge criteria
hbear 6/29/2007 4:32:00 PM
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.

"Peter Jamieson" wrote:

[Quoted Text]
> 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
>
> "hbear" <hbear[ at ]discussions.microsoft.com> wrote in message
> news:3C1FF5B8-260B-462F-8A4C-4C2CB12273F7[ at ]microsoft.com...
> > 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!
> >
>
>
Re: tricky IF merge criteria
"Peter Jamieson" <pjj[ at ]KillmapSpjjnet.demon.co.uk> 6/29/2007 4:52:59 PM
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
"hbear" <hbear[ at ]discussions.microsoft.com> wrote in message
news:FF1B212C-4E3B-478B-A8B9-851F7BA02D44[ at ]microsoft.com...
[Quoted Text]
> 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.
>
> "Peter Jamieson" wrote:
>
>> 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
>>
>> "hbear" <hbear[ at ]discussions.microsoft.com> wrote in message
>> news:3C1FF5B8-260B-462F-8A4C-4C2CB12273F7[ at ]microsoft.com...
>> > 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!
>> >
>>
>>

Re: tricky IF merge criteria
hbear 6/29/2007 6:28:01 PM
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


"Peter Jamieson" wrote:

[Quoted Text]
> 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
> "hbear" <hbear[ at ]discussions.microsoft.com> wrote in message
> news:FF1B212C-4E3B-478B-A8B9-851F7BA02D44[ at ]microsoft.com...
> > 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.
> >
> > "Peter Jamieson" wrote:
> >
> >> 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
> >>
> >> "hbear" <hbear[ at ]discussions.microsoft.com> wrote in message
> >> news:3C1FF5B8-260B-462F-8A4C-4C2CB12273F7[ at ]microsoft.com...
> >> > 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!
> >> >
> >>
> >>
>
>

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net