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 : 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. ' Copyright : The following may be altered and reused as you wish so long as the ' copyright notice is left unchanged (including Author, Website and ' Copyright). It may not be sold/resold or reposted on other sites (links ' back to this site are allowed). ' ' Revision History: ' Rev Date(yyyy/mm/dd) Description ' ************************************************************************************** ' 1 2010-Sep-10 Initial Release '--------------------------------------------------------------------------------------- Function EnumerateDocFrmFlds(sFileName As String) On Error GoTo Error_Handler 'Requires a reference to the Word object library Dim oApp As Word.application Dim oDoc As Word.Document Dim dFormField As 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 0 Set oDoc = oApp.Documents.Open(sFileName) oApp.Visible = False 'Control whether or not Word becomes 'visible to the user 'Loop through each form field For Each dFormField In oDoc.FormFields() Debug.Print dFormField.Name Next Error_Handler_Exit: On Error Resume Next oDoc.Close False oApp.Quit Set oDoc = Nothing Set oApp = Nothing Exit Function Error_Handler: MsgBox "The following error has occured." & vbCrLf & vbCrLf & _ " Error Number: " & Err.Number & vbCrLf & _ " Error Source: EnumerateDocFrmFlds" & vbCrLf & _ " Error Description: " & Err.Description, _ vbCritical, "An Error has Occured!" Resume Error_Handler_Exit End Function

Friday, September 10th, 2010, 4:15 pm | 

