Class MailMergeDataField (Word VBA)

The class MailMergeDataField represents a single mail merge field in a data source. The MailMergeDataField object is a member of the MailMergeDataFields collection. The MailMergeDataFields collection includes all the data fields in a mail merge data source (for example, Name, Address, and City).


To use a MailMergeDataField class variable it first needs to be instantiated, for example

Dim mmd as MailMergeDataField
Set mmd = ActiveDocument.MailMerge.DataSource.DataFields(Index:=1)

The following procedures can be used to set variables of type MailMergeDataField: MailMergeDataFields.Item and MailMergeDataSource.DataFields

For Each

Here is an example of processing the MailMergeDataField items in a collection.

Dim mmdDataField As MailMergeDataField
For Each mmdDataField In ActiveDocument.MailMerge.DataSource.DataFields
Next mmdDataField


Returns a Long that represents the position of an item in a collection.

Dim lngIndex As Long
lngIndex = ActiveDocument.MailMerge.DataSource.DataFields(1).Index


Returns name of the specified object.

Dim strName As String
strName = ActiveDocument.MailMerge.DataSource.DataFields(1).Name


Returns an object that represents the parent object of the specified MailMergeDataField object.

Dim objParent As Object
Set objParent = ActiveDocument.MailMerge.DataSource.DataFields(1).Parent


Returns the contents of the mail merge data field or mapped data field for the current record.

Use the ActiveRecord property to set the active record in a mail merge data source.

For Each dataF In _ 
 If dataF.Value <> "" Then dRecord = dRecord & _ 
 dataF.Value & vbCr 
Next dataF 
MsgBox dRecord


Returns a Long that represents the number of mail merge data fields in the collection.

Dim lngCount As Long
lngCount = ActiveDocument.MailMerge.DataSource.DataFields.Count


Returns an individual MailMergeDataField object in a collection.

Syntax : expression.Item (Index)

Index: The individual object to be returned. Can be a Long indicating the ordinal position or a String representing the name of the individual object.

Dim mmd As MailMergeDataField
Set mmd = ActiveDocument.MailMerge.DataSource.DataFields(Index:=1)


Returns an object that represents the parent object of the specified MailMergeDataFields object.

Dim objParent As Object
Set objParent = ActiveDocument.MailMerge.DataSource.DataFields.Parent