How to get mail merge fields in text boxes?

D

DYang

Hi,

I am working on a project that needs to find all the mail merge fields in a Word template. C#, Word XP InterOp, and Word XP are used in the project.

To get the mail merge fields in the main document can be done through ActiveDocument.MailMerge.Fields. It returns a collection of the codes of all mail merge fields in the active document. But it does not give you the mail merge fields inside a textbox, header, or footer.

Does any know how to get the mail merge field codes in a text box?

Thanks a lot

Daniel
 
C

Chad DeMeyer

DYang,

Dim s As Shape, f As Field
For Each s In ActiveDocument.Shapes
With s.TextFrame
If .HasText Then
For Each f In .TextRange.Fields
Select Case f.Type
Case wdFieldMergeField, wdFieldMergeRec, wdFieldMergeSeq
'your code here
Case Else
'your code here
End Select
Next f
End If
End With
Next

Hope that helps
Regards,
Chad
 

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