How to get the Names of the MailMerge Fields in the document(word 2003)?

R

Ram

I am automating MailMerge and do not want to process the document if
the user has not supplied all the data needed to perform MailMerge.
This is the only way I can think of for suppressing the Message boxes
since the C# application runs as a service. Is there a way we can find
the names of the MailMerge fields after loading the document?

Can anyone help?

Thanks,
Ram
 
P

Peter Jamieson

You can get the names of the fields in the data source by looking at the
ActiveDocument.MailMerge.DataSource.DataFields collection.

For the names of fields used in the document, you could try something like
the following (after converting to C# of course). It inspects /all/ field
types. I wouldn't guarantee that it will find them all (e.g. it may not get
everything in the graphics layer) but it's the nearest I've ever got.

Sub IterateMergeFields()
Dim r As Range
Dim f As Field
Dim s As Shape
For Each r In ActiveDocument.StoryRanges
If r.StoryType <> wdTextFrameStory Then
For Each f In r.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
For Each s In ActiveDocument.Shapes
If s.Type = msoTextBox Then
For Each f In s.TextFrame.TextRange.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
End Sub

I would be interested to know if you have found a reliable way to deal with
mail merge main documents that cannot find their data source - typically
there is nothing you can do to prevent Word displaying a dialog box when
that occurs.
 

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