Rename/Sequence Formfields

Home Up Odds & Ends Photo Gallery Search Contact Me Privacy Notice

 

 

The information in this website is provided without risk or obligation and free of charge.  However, if you have benefitted from my efforts here and would like to make a contribution to help me continue and maintain this work then any donation will be greatly appreciated. Please click the adjacent button to access PayPal.  Thank you.
 

This Microsoft Word Help and Tips Page provides a VBA method to globally name/rename and sequence formfields in a document.

Whenever you create a new formfield Word automatically assigns a default bookmark designation to the formfield. The default designation is alphanumeric and determined by the type of formfield and the number of pre-existing formfields of the same type in the document.

For example, if you open and new blank document and insert a textinput formfield, a checkbox formfield, and a dropdown formfield, the document will contain three bookmarks:

If you then insert a second textinput formfield after the first textinput formfield, Word will automatically designated this new formfield as Text2.   Similar results occur if you insert additional checkboxes or dropdowns after existing formfields in the document. 

A problem arises when you insert new formfields before or in between existing formfields.  While Word gets the “alpha” portion of the designation correct, the numeric part is strictly sequential and therefore appears to get out of whack.  For example, if you insert a third textinput formfield between the two previously mentioned it will be designated text3 (i.e., Word does not re-sequence the existing form fields so that the fields appear sequential in the document). 

A second problem occurs when you copy and paste existing formfields in a document.  Regardless of where formfields are pasted these “cloned” fields have no bookmark designation at all.

If you have ever created a form contains lots of formfields, then you know that nothing is faster than copying and pasting the formfields.  You will also know that it is then a slow and laborious process to manually select and bookmark each one. 

 The following macro will do this task for you automatically and as it cycles through each formfield in a document it will automatically reassign the proper default bookmark designation

  Sub GlobalRenameFormFields()
Dim oFrmFlds As FormFields
Dim pIndex As Long
Dim i As Long
Dim j As Long
Dim k As Long
Dim oVar As Variant
pIndex = 0
i = 0
j = 0
k = 0
If ActiveDocument.ProtectionType <> wdNoProtection Then
  ActiveDocument.Unprotect
End If
Set oFrmFlds = ActiveDocument.FormFields
For pIndex = 1 To oFrmFlds.Count
  oFrmFlds(pIndex).Select
  Select Case oFrmFlds(pIndex).Type
    Case wdFieldFormTextInput
      oVar = oFrmFlds(pIndex).Result
      i = i + 1
      With Dialogs(wdDialogFormFieldOptions)
        .Name = "Text" & i

        .Execute
      End With
      oFrmFlds(pIndex).Result = oVar
    Case wdFieldFormCheckBox
      oVar = oFrmFlds(pIndex).CheckBox.Value
      j = j + 1
      With Dialogs(wdDialogFormFieldOptions)
        .Name = "Check" & j
        .Execute
      End With
      oFrmFlds(pIndex).CheckBox.Value = oVar
    Case wdFieldFormDropDown
      oVar = oFrmFlds(pIndex).DropDown.Value
      k = k + 1
      With Dialogs(wdDialogFormFieldOptions)
        .Name = "DropDown" & k
        .Execute
      End With
      oFrmFlds(pIndex).DropDown.Value = oVar
    Case Else
    
 'Do Nothing
  End Select
Next pIndex
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
 

After running the macro, all existing formfields are named and indexed with default designations.

 

 

Need help applying macros?  See fellow MVP Graham Mayor's  Guide for Installing Macros


Looking for something else?

Google