How to do String Manipulations on a Merge Field?

L

Lynda

Is it possible to find the last or first character(s) in a mail merge field?
For example, I want to check a merge field that contains 4 characters and I
just want to do an if-then-else statement on the final character. For
example, if Right(mergefield) = 1, then do something, else do something
different. I can't seem to find any information on string manipulations in
Word. I am using Word 2003.

Thanks for your help.

Lynda
 
P

Peter Jamieson

There are no string functions such as left(), mid(), right() but you can use
wildcards at the left hand eend or right hand end of the second comparand
(but not both ends at once), e.g.

{ IF "{ MERGEFIELD myfield }" = "*1" "the rightmost character is a 1" "the
rightmost character is not a 1" }

It doesn't work for text longer than around 128 characters (or maybe 64, I
forget).
 
M

macropod

Hi Lynda,

Alternatively, to check that the string is exactly 4 characters long and ends in a 1:
{ IF "{ MERGEFIELD myfield }" = "???1" "the last character is a 1 at the 4th position" "the last character is NOT a 1 at the 4th
position" }
or to check the 4th character in a potentially longer string:
{ IF "{ MERGEFIELD myfield }" = "???1*" "the 4th character is a 1 " "the 4th character is NOT a 1" }

Cheers
 
L

Lynda

Thanks so much! That is exactly what I was looking for.

Lynda

macropod said:
Hi Lynda,

Alternatively, to check that the string is exactly 4 characters long and
ends in a 1:
{ IF "{ MERGEFIELD myfield }" = "???1" "the last character is a 1 at the
4th position" "the last character is NOT a 1 at the 4th position" }
or to check the 4th character in a potentially longer string:
{ IF "{ MERGEFIELD myfield }" = "???1*" "the 4th character is a 1 " "the
4th character is NOT a 1" }

Cheers
 

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