Class MailMergeField (Word VBA)
The class MailMergeField represents a single mail merge field in a document. The MailMergeDataField object is a member of the MailMergeDataFields collection. The MailMergeDataFields collection includes all the mail merge related fields in a document.
Suggestion: Our 4TOPS Document Creation using Microsoft Access is a VBA based alternative for creating documents. In addition to Word it also supports Excel and PowerPoint. (Comparing 4TOPS Document Creation with Word Mail Merge
To use a MailMergeField class variable it first needs to be instantiated, for example
Dim mmf as MailMergeField
Set mmf = ActiveDocument.MailMerge.Fields(Index:=1)
The following procedures can be used to set variables of type MailMergeField: Next, Previous, MailMergeFields.Add, MailMergeFields.AddAsk, MailMergeFields.AddFillIn, MailMergeFields.AddIf, MailMergeFields.AddMergeRec, MailMergeFields.AddMergeSeq, MailMergeFields.AddNext, MailMergeFields.AddNextIf, MailMergeFields.AddSet, MailMergeFields.AddSkipIf and MailMergeFields.Item.
Code
Returns a Range object that represents a field's code.
A field's code is everything that's enclosed by the field characters ({ }) including the leading and trailing space characters. You can access a field's code without changing the view from field results.
Dim fieldLoop As Field
For Each fieldLoop In ActiveDocument.Fields
MsgBox Chr(34) & fieldLoop.Code.Text & Chr(34)
Next fieldLoop
Copy
Copies the specified mail merge field to the Clipboard.
ActiveDocument.MailMerge.Fields(1).Copy
Cut
Removes the specified mail merge field from the document and moves it to the Clipboard.
If ActiveDocument.Fields.Count >= 1 Then
ActiveDocument.Fields(1).Cut
Selection.Collapse Direction:=wdCollapseEnd
Selection.Paste
End If
Delete
Deletes the specified mail merge field.
ActiveDocument.MailMerge.Fields(1).Delete
Locked
True if the specified field is locked.
When a field is locked, you cannot update the field results.
ActiveDocument.MailMerge.Fields(1).Locked = True
Next
Returns the next mail merge field in the collection of mail merge fields.
Dim mmfNext As MailMergeField
Set mmfNext = ActiveDocument.MailMerge.Fields(1).Next
Previous
Returns the previous mail merge field in the collection of mail merge fields.
Dim mmfPrevious As MailMergeField
Set mmfPrevious = ActiveDocument.MailMerge.Fields(1).Previous
Select
Selects the specified mail merge field.
After using this method, use the Selection property to work with the selected items. For more information, see Working with the Selection Object.
ActiveDocument.MailMerge.Fields(1).Select
Type
Returns the field type. Here you can find possible values for
Dim wftType As WdFieldType
wftType = ActiveDocument.MailMerge.Fields(1).Type