Class MappedDataField (Word VBA)

A mapped data field is a field contained within Microsoft Word that represents commonly used name or address information, such as "First Name." If a data source contains a "First Name" field or a variation (such as "First_Name," "FirstName," "First," or "FName"), the field in the data source will automatically map to the corresponding mapped data field in Word. If a document or template is to be merged with more than one data source, mapped data fields make it unnecessary to reenter the fields into the document to agree with the field names in the database.

Set

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

Dim mdf as MappedDataField
Set mdf = ActiveDocument.MailMerge.DataSource.MappedDataFields(Index:=1)

The following procedures can be used to set variables of type MappedDataField: MappedDataFields.Item and MailMergeDataSource.MappedDataFields

For Each

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

Dim mdf As MappedDataField
For Each mdf In ActiveDocument.MailMerge.DataSource.MappedDataFields
	
Next mdf

DataFieldIndex

Returns or sets a Long that represents the corresponding field number in the mail merge data source to which a mapped data field maps.

This property returns zero if the specified data field is not mapped to a mapped data field.

Sub MapField() 
 With ActiveDocument.MailMerge.DataSource 
 .MappedDataFields(wdAddress1).DataFieldIndex = _ 
 .FieldNames("PostalAddress1").Index 
 End With 
End Sub

DataFieldName

Sets or returns a String that represents the name of the field in the mail merge data source to which a mapped data field maps.

A blank string is returned if the specified data field is not mapped to a mapped data field.

Sub MappedFields() 
 Dim intCount As Integer 
 Dim docCurrent As Document 
 Dim docNew As Document 
 
 On Error Resume Next 
 
 Set docCurrent = ActiveDocument 
 Set docNew = Documents.Add 
 
 'Add leader tab to new document 
 docNew.Paragraphs.TabStops.Add _ 
 Position:=InchesToPoints(3.5), _ 
 Leader:=wdTabLeaderDots 
 
 With docCurrent.MailMerge.DataSource 
 
 'Insert heading paragraph for tabbed columns 
 docNew.Content.InsertAfter "Word Mapped Data Field" _ 
 & vbTab & "Data Source Field" 
 
 Do 
 intCount = intCount + 1 
 
 'Insert Word mapped data field name and the 
 'corresponding data source field name 
 docNew.Content.InsertAfter .MappedDataFields( _ 
 Index:=intCount).Name & vbTab & _ 
 .MappedDataFields(Index:=intCount) _ 
 .DataFieldName 
 
 'Insert paragraph 
 docNew.Content.InsertParagraphAfter 
 Loop Until intCount = .MappedDataFields.Count 
 
 End With 
 
End Sub

Index

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

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

Name

Returns name of the specified object.

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

Parent

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

Dim objParent As Object
Set objParent = ActiveDocument.MailMerge.DataSource.MappedDataFields(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

MappedDataFields.Count

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

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

MappedDataFields.Item

Returns an individual MappedDataField object.

Syntax : expression.Item (Index)

Index: The specified mapped data field.

Dim mdf As MappedDataField
Set mdf = ActiveDocument.MailMerge.DataSource.MappedDataFields(Index:=1)

MappedDataFields.Parent

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

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