VBA – Word – Enumerate/List All the Document Form Fields

Have you ever started coding some vba to manipulate a Word document’s form fields and started going back and forth between the word document and the VBE. This can work if you have a few form fields, but becomes very tiresome when dealing with large form. As such, I created a very simple procedure to extract a list of the form fields in one shot and then I could continue my work in peace. I hope the following saves you some time and frustrations too.

'---------------------------------------------------------------------------------------
' Procedure : EnumerateDocFrmFlds
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Generate a listing of all of the form fields containing within the
'             specified word document and print them to the immediate window.
'               These are the Legacy Form Controls, not the newer Content Controls
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFileName   Fully qualified path, filename and extension of the word document to
'               Extract a list of form fields from
'
' Usage:
' ~~~~~~
' Call EnumerateDocFrmFlds("C:\Temp\Test.docx")
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2010-09-10              Initial Release
' 2         2019-01-24              Updated coypright
'                                   Added Input Variables
'                                   Added Usage
'                                   Switched to Late Binding
'---------------------------------------------------------------------------------------
Function EnumerateDocFrmFlds(ByVal sFileName As String)
    On Error GoTo Error_Handler
    Dim oApp                  As Object    'Word.Application
    Dim oDoc                  As Object    'Word.Document
    Dim oFormField            As Object    'FormField

    On Error Resume Next
    Set oApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then    'Word isn't running so start it
        Set oApp = CreateObject("Word.Application")
    End If
    On Error GoTo Error_Handler

    Set oDoc = oApp.Documents.Open(sFileName)
    oApp.Visible = False    'Control whether or not Word becomes visible to the user

    For Each oFormField In oDoc.FormFields 'Loop through each form field
        Debug.Print oFormField.Name
    Next

Error_Handler_Exit:
    On Error Resume Next
    If Not oFormField Is Nothing Then Set oFormField = Nothing
    If Not oDoc Is Nothing Then
        oDoc.Close False
        Set oDoc = Nothing
    End If
    If Not oApp Is Nothing Then
        oApp.Quit
        Set oApp = Nothing
    End If
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: EnumerateDocFrmFlds" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function