Form Fields


Dim control As CommandBarControl 
Set control = Application.CommandBars.FindControl(ID:=1605)
return control.state

The Protect Form button on the Forms toolbar is a shortcut for (Tools > Protection) and select Filling in Forms.


The FormFields collection is 1 based.


Resetting the Form Fields

The form fields will be automatically reset when the following operations are performed:
A datasource is attached to a document.
The document is protected and the "NoReset" parameter is not explicitly set to True.



How to assign a name to a FormField that does not have a name

The following line causes an error if the formfield does not already have a name

Selection.FormFields(1).Name = "myname" 

The following code does work

With Dialogs(wdWordDialog.wdDialogFormFieldOptions) 
   .Name = "Myname"
   .Execute
End With


How to find the name of the current formfield

If you need to know which formfield the user tabbed out of in a macro you are using as an on-exit macro you need to take into account that you cant always simply use the command Selection.FormFields(1).Name.
This line will not work with a text formfield although it works with checkboxes and drop-downs.
If you need code to find which formfield was tabbed out of, that works with every type of formfield

If Selection.FormFields.Count = 1 Then 
'no text box but a checkbox or drop-down
   Call MsgBox(Selection.FormFields(1).Name)

ElseIf Selection.FormFields.Count = 0 And _
       Selection.Bookmarks.Count > 0 Then
   Call MsgBox(Selection.Bookmarks(Selection.Bookmarks.Count).Name))
End If


How to hide a "Print" commandbutton on a document form when a user clicks on it

When creating a document/template form you may want to include a commandbutton on the document to print the form
But how do you hide the commandbutton so it is not printed
1) Insert a "drawing" textbox and maybe set the borders
2) Insert a commandbutton in the document from the Control toolbox toolbar
3) Right click the commandbutton and change the caption to "Print Document"
4) Cut the commandbutton and paste it into the textbox
5) Make sure you are in Design Mode
6) Double click the commandbutton and add the following code

Private Sub CommandButton_Click() 
   With ActiveDocument
      .Shapes(1).Visible = msoFalse
      .PrintOut Background:=False
      .Shapes(1).Visible = msoTrue
   End With
End Sub

Inserting a Form Field

Selection.FormFields.Add Range:=Selection.Range, _ 
                         Type:=wdFieldType.wdFieldFormTextInput


Selecting a Form Field

If you want to select a form field using a macro, you can use a variety of different methods
These two lines work but if the form field is empty then it will become highlighted

ActiveDocument.FormFields("Text1").Range.Select 
ActiveDocument.FormFields("Text1").Select

This line will select the form field (similar to if you had pressed Tab) even when it is empty.

Selection.GoTo What:=wdGoToBookmark, Name:="Text1" 

This line will often cause the screen to jump in a really annoying way depending on the vertical position of the fields.


This line will work and will highlight the entire field.
The screen will only jump if the field is off the screen.

ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Select 


How to find the name of the current form field


If you need to know which formfield the user tabbed out of in a macro you're using as on-exit macro you need to take into account that you can't always simply use the command Selection.Formfields(1).Name. This line won't work with a textformfield although it works with checkboxes and dropdowns.
If you need code to find out which formfield was tabbed out of, that works with every type of formfield, use:


If Selection.FormFields.Count = 1 Then 
'No textbox but a check- or listbox
    MsgBox Selection.FormFields(1).Name
ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
    MsgBox Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If

How to set the result of a text formfield using VBA, if the string is longer then 256 characters


You get an error: "String too long" (a ridiculous "design" feature, given that you can do it manually without problems!).

Dim FmFld As FormField, Str1 As String 
Str1 = (a long string > 256 characters)

Set FmFld = ActiveDocument.FormFields(1)
FmFld.Result = Str1

Same if you use:

ActiveDocument.Formfields("Text1").Result = Str1 

You can get round this by using:

ActiveDocument.Unprotect 
FmFld.Range.Fields(1).Result.Text = Str1
ActiveDocument.Protect Type:=wdProtectionType.wdAllowOnlyFormFields, _
                       NoReset:=True

Or if you're referring to the formfield by name:

ActiveDocument.Unprotect 
ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Text = Str1
ActiveDocument.Protect Type:=wdProtectionType.wdAllowOnlyFormFields, _
                       NoReset:=True


How to set the result of a text formfield in a Word 2000 table, using VBA, if the string contains carriage returns


The following works in Word 97:

ActiveDocument.FormFields("Text2").Result = "Line1" & vbCr + "Line2" 

Unfortunately in Word 2000, if the formfield is in a table, the carriage returns are converted to invalid ascii characters:


There are two workarounds:
One is to use line breaks instead of paragraph marks. The following works in Word 97 and above:

ActiveDocument.FormFields("Text1").Result = "Line1" & Chr$(11) & "Line2" 

Or you can use the strangely named vbVerticalTab constant instead of Chr$(11)

ActiveDocument.FormFields("Text1").Result = "Line1" & vbVerticalTab & "Line2" 


The other workaround is to use the following:

ActiveDocument.Unprotect 
ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Text = "Line1" & vbCr + "Line2"
ActiveDocument.Protect Type:=wdProtectionType.wdAllowOnlyFormFields, _
                       NoReset:=True

This also works in all versions of Word VBA. As well as allowing you to use real paragraph marks, this method has the advantage that it works with strings longer than 256 characters; but the disadvantage that it you have to unprotect and reprotect.


Changing the Tab Order of Form Fields

Word automatically decides the tabbing sequence for your form fields.
Lets consider that you have put your form fields into a table, you might want your users to tab through all the fields in column 1, then column 2, then column 3; whereas by default, they will tab through the formfields in row 1, then row 2, then row 3.
You can get around this by using a macro although it does mean that you will not be able to use the mouse to select which form field you want to change.
A userform in this situation should be used.
The UserForm code could fill in the document form (with or without using form fields in the document); and the tab-order of UserForm controls is very easy to set.


Creating an OnExit macro for each form field

Always give your bookmarks meaningful names.

Option Explicit 

Sub ExitBetter1()
    ActiveDocument.Bookmarks("Better3").Range.Fields(1).Result.Select
End Sub

Sub ExitBetter2()
    ActiveDocument.Bookmarks("Better1").Range.Fields(1).Result.Select
End Sub

Sub ExitBetter3()
    ActiveDocument.Bookmarks("Better2").Range.Fields(1).Result.Select
End Sub


Assign a single OnExit macro to all your form fields

If you have a lot of form fields you might find it easier to create a single macro to set the tab-order, assigned as the Exit macro for every formfield, as follows:

Sub ChangeTabOrder() 
Dim sCurrentFormField As String,
Dim sFormFieldGoTo As String

'First get the name of the current formfield
If (Selection.FormFields.Count = 1) Then
'No textbox but a check- or listbox
    sCurrentFormField = Selection.FormFields(1).Name

ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
'Textbox
    sCurrentFormField = Selection.Bookmarks(Selection.Bookmarks.Count).Name
End If

'Then find out which formfield to go to next ...
Select Case StrCurFFld
     Case "Better1" : sFormFieldGoTo = "Better3"
     Case "Better2" : sFormFieldGoTo = "Better1"
     Case "Better3" : sFormFieldGoTo = "Better2"
 End Select

ActiveDocument.Bookmarks(sFormFieldGoTo).Range.Fields(1).Result.Select
End Sub


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext