Fields.Add method (Word)
Adds a Field object to the Fields collection. Returns the Field object at the specified range.
expression.Add (Range, Type, Text, PreserveFormatting)
expression Required. A variable that represents a 'Fields' collection.
Name | Required/Optional | Data type | Description |
---|---|---|---|
Range | Required | Range object | The range where you want to add the field. If the range isn't collapsed, the field replaces the range. |
Type | Optional | Variant | Can be any WdFieldType constant. For a list of valid constants, consult the Object Browser. The default value is wdFieldEmpty. |
Text | Optional | Variant | Additional text needed for the field. For example, if you want to specify a switch for the field, you would add it here. |
PreserveFormatting | Optional | Variant | True to have the formatting that's applied to the field preserved during updates. |
Field
You cannot insert some fields (such as wdFieldOCX and wdFieldFormCheckBox) by using the Add method of the Fields collection. Instead, you must use specific methods such as the AddOLEControl method and the Add method for the FormFields collection.
This example inserts a USERNAME field at the beginning of the selection.
Selection.Collapse Direction:=wdCollapseStart
Set myField = ActiveDocument.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldUserName)
This example inserts a LISTNUM field at the end of the selection. The starting switch is set to begin at 3.
Selection.Collapse Direction:=wdCollapseEnd
ActiveDocument.Fields.Add Range:=Selection.Range, _
Type:=wdFieldListNum, Text:="\s 3"
This 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
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.