MailMergeFieldNames Collection Object

Microsoft Word Visual Basic

MailMergeFieldNames Collection Object

         
Documents (Document) MailMerge
MailMergeDataSource
MailMergeFieldNames (MailMergeFieldName)

A collection of MailMergeFieldName objects that represent the field names in a mail merge data source.

Using the MailMergeFieldNames Collection

Use the FieldNames property to return the MailMergeFieldNames collection. The following example displays the names of the fields in the data source attached to the active document.

For Each afield In ActiveDocument.MailMerge.DataSource.FieldNames
    MsgBox afield.Name
Next afield

You cannot add names to the MailMergeFieldNames collection. When a field is added to a data source, the field name is automatically included in the MailMergeFieldNames collection. Use the EditDataSource method to edit the contents of a data source. The following example adds a data field named "Author" to a table in the data source attached to the active document.

If ActiveDocument.MailMerge.DataSource.Type = _
        wdMergeInfoFromWord Then
    ActiveDocument.MailMerge.EditDataSource
    With ActiveDocument.Tables(1)
        .Columns.Add
        .Cell(Row:=1, Column:=.Columns.Count).Range.Text = "Author"
    End With
End If