Partager via


Fields Collection Object (Word)

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

Remarks

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

Note

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

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

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.

See Also

Concepts

Word Object Model Reference

Fields Object Members