Fields Collection

Microsoft Publisher Visual Basic

Fields Collection

TextRange Fields
Field
Multiple objects

A collection of Field objects that represent all the fields in a text range.

Using the Fields Collection

Use the Fields property to return the Fields collection. 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 publication. The following example displays the field code and the result of the first field in each text box in the active publication.

Sub ShowFieldCodes()
    Dim pagPage As Page
    Dim shpShape As Shape

    For Each pagPage In ActiveDocument.Pages
        For Each shpShape In pagPage.Shapes
            If shpShape.Type = pbTextFrame Then
                With shpShape.TextFrame.TextRange
                    If .Fields.Count > 0 Then
                        MsgBox "Code =  " & .Fields(1).Code & vbLf _
                            & "Result =  " & .Fields(1).Result & vbLf
                    End If
                End With
            End If
        Next
    Next
End Sub
		

The Count property for this collection in a publication returns the number of items in a specified shape or selection.