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).

Set

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

Index

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

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

Name

Returns name of the specified object.

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

Parent

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

Value

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 _ 
 Documents("Main.doc").MailMerge.DataSource.DataFields 
 If dataF.Value <> "" Then dRecord = dRecord & _ 
 dataF.Value & vbCr 
Next dataF 
MsgBox dRecord

MailMergeDataFields.Count

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

MailMergeDataFields.Item

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)

MailMergeDataFields.Parent

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

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