Class Fields (Word VBA)

A collection of Field objects that represent all the fields in a selection, range, or document. To use a Fields class variable it first needs to be instantiated, for example


Dim flds as Fields
Set flds = ActiveDocument.Fields

For Each

Here is an example of processing the Fields items in a collection.


Dim fld As Field
For Each fld In ActiveDocument.Fields
	
Next fld

Add

Adds a Field object to the Fields collection. Returns the Field object at the specified range.

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.

Add (Range, Type, Text, PreserveFormatting)


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

Arguments

The following argument is required

Range (Range) - The range where you want to add the field. If the range isn't collapsed, the field replaces the range.

Optional arguments

The following arguments are optional

Type (WdFieldType) - Can be any WdFieldType constant. For a list of valid constants, consult the Object Browser. The default value is wdFieldEmpty.

Here you can find possible values for WdFieldType

Text (String) - Additional text needed for the field. For example, if you want to specify a switch for the field, you would add it here.

PreserveFormatting (Boolean) - True to have the formatting that's applied to the field preserved during updates.

Count

Returns a Long that represents the number of fields in the collection.


Dim lngCount As Long
lngCount = ActiveDocument.Fields.Count

Item

Returns an individual Field object in a collection.

Item (Index)

Index: The individual object to be returned. Can be a Long indicating the ordinal position of the individual object.


Dim fld As Field
Set fld = ActiveDocument.Fields(Index:=1)

Locked

True if all fields in the Fields collection are locked.

This property can be True, False, or wdUndefined (if some of the fields in the collection are locked and others are not).


Selection.Fields.Locked = True

ToggleShowCodes

Switches the display of the fields between field codes and field results. Use the ShowCodes property to control the display of an individual field.


Selection.Fields.ToggleShowCodes

Replaces all the fields in the Fields collection with their most recent results.

When you unlink a field, the current result is converted to text or a graphic and can no longer be updated automatically. Note that some fields—such as XE (Index Entry) fields and SEQ (Sequence) fields—cannot be unlinked.


With ActiveDocument.Sections(1).Range.Fields 
 .Update 
 .Unlink 
End With

Update

Updates the result of the fields object.

Returns 0 (zero) if no errors occur when the fields are updated, or returns a Long that represents the index of the first field that contains an error.


If ActiveDocument.Fields.Update = 0 Then 
 MsgBox "Update Successful" 
Else 
 MsgBox "Field " & ActiveDocument.Fields.Update & _ 
 " has an error" 
End If

UpdateSource

Saves the changes made to the results of an INCLUDETEXT field back to the source document.

The source document must be formatted as a Word document.


ActiveDocument.Fields.UpdateSource