Fields Collection Object

Microsoft Word Visual Basic

Multiple objectsFields
Field
Multiple objects

A collection of Field objects that represent all the fields in a selection, range, or document.

Using the Fields Collection

Use the Fields property to return the Fields collection. The following example updates all the fields in the selection.

Selection.Fields.Update
		

Use the Add method to add a field to the Fields collection. The following example inserts a DATE field at the beginning of the selection and then displays the result.

Selection.Collapse Direction:=wdCollapseStart
Set myField = ActiveDocument.Fields.Add(Range:=Selection.Range, _
    Type:=wdFieldDate)
MsgBox myField.Result
		

Use Fields(index), where index is the index number, to return a single Field object. The index number represents the position of the field in the selection, range, or document. The following example displays the field code and the result of the first field in the active document.

If ActiveDocument.Fields.Count >= 1 Then
    MsgBox "Code =  " & ActiveDocument.Fields(1).Code & vbCr _
        & "Result =  " & ActiveDocument.Fields(1).Result & vbCr
End If
		

Remarks

Use the Fields property with a MailMerge object to return the MailMergeFields collection.

The Count property for this collection in a document returns the number of items in the main story only. To count items in other stories use the collection with the Range object.