get mailmerge fields in an IF formula

G

Gary Wu

Hi,

I use Word.MailMerge.Fields to get the set of fields in a document. One
of the fields is an IF formula that contains a couple of mailmerge
fields.

When I loop through the fields, for each field, I use
Word.MailMergeField.Code.Text to get the field code. When it loops to
the IF statement, I get the whole if statement instead of the fields
contained in the if statement as the field code. Is there a way to get
the fields contained in an if statement besides string parsing?
Thanks,
Gary
 
P

Peter Jamieson

There are at least two collections: fields, and mailmerge fields. Not all
fields are mailmerge fields. If, for example, your IF field looks like

{ IF { MERGEFIELD a } = 0 "{ MERGEFIELD b }" "{ MERGEFIELD c }" }

then your iteration or MailMergeFields would give you

IF  MERGEFIELD a  = 0 " MERGEFIELD b " " MERGEFIELD c "
MERGEFIELD a
MERGEFIELD b
MERGEFIELD c

whereas

{ IF { MERGEFIELD a } = 0 "{ b }" "{ c }" }

would give you

IF  MERGEFIELD a  = 0 " b " " c "
MERGEFIELD a

If you use the ActiveDocument.Fields collection (notice that the Field type
is different from the MailMergeField type), you will see the same result for
the first IF statement but

IF  MERGEFIELD a  = 0 " b " " c "
MERGEFIELD a
b
c

for the second.

Peter Jamieson
 
G

Gary Wu

Peter, thank you very much! With the information you provided, I solved
my problem. What I wanted to achieve is to replace mailmerge fields in
one application with corresponding mailmerge fields in another and at
the same time reserve all the logic in formulas.

So, what I did to achieve that is for each mailmerge field, I check its
type like this

Word.MailMergeField field = ... //a reference to a mailmerge field
if (field.Type == Word.WdFieldType.wdFieldMergeField)
{
//replace the field with a corresponding field.
}

Formulas like an IF statement is of type wdFieldFormula and mailmerge
fields contained in an IF statement is of type wdFieldMergeField. So
with the above code, I can replace only mailmerge fields while keeping
the logic in formulas intact.
Thanks Peter again for your helpful information!
 

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