Number of Merge Fields in Header / Footer

N

Neil Dittmar

Hello All,

I'm trying to determine the number of merge fields present
in a header or footer of a Word document. Seems that the
Word 2003 Object doesn't pick them up with the following
line of code:

wdApp.Documents(1).MailMerge.Fields.Count

Even if there are mail merge fields in the header or
footer, this still evaluates to 0 if there are none in the
main part of the document. I have tried to determine the
necessary code using macros, but I just can't seem to get
it to work. Any help would be greatly appreciated.

Thanks in advance,

Neil Dittmar
Software Developer
Aurora Information Systems
 
S

Stephanie Krieger

Hi, Neil,

Sure -- you have to have the code look separately in the
headers and footers.

Headers and footers are managed by the Sections
collection object.

So, for example, let's say you wanted to count the number
of fields in the primary header of the first section of
the document ... here's that line of code:

ActiveDocument.Sections(1).Headers
(wdHeaderFooterPrimary).Range.Fields.Count

You can use the "Type" property of the Fields collection
object to determine if they are any of the merge field
types you're looking for ... (search wdfield in the
Object browser for a complete list of field type
constants).

You can loop through sections and headers & footers (note
that there are 3 types each of headers and footers in VBA
(to look through primary, first page, odd\even headers
and footers separately).

Hope that helps.

Stephanie Krieger
author of Microsoft Office Document Designer (from
Microsoft Press)
email: MODD_2003 at msn dot com
blog: arouet.net
 

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