Hyperlinks inserted directly into a document protected as a form are unavailable to the user. But they can be placed in a MacroButton field as the display text, and one macro can be used to follow all hyperlinks. Note that the hyperlink will be treated like plain text (the mouse pointer shape will not change to a hand over the link).
Syntax for the MacroButton field:
{ MACROBUTTON FollowHLink "http://www.TheHyperlink" }
VBA Code to follow the hyperlink:
Sub FollowHLink()
Selection.Hyperlinks(1).Follow
End Sub
Various features don't work in a protected form, including Edit/Find. Here, again, it's possible to use a macro to emulate at least part of the built-in functionality.
Sub FindInForm()
Dim doc As Word.Document, ffld As Word.FormField
Dim szFindTerm As String, lPos As Long
Dim szSearchString As String, rng As Word.Range
Dim rngStart As Word.Range, lNumFfld As Long
Dim lFfldCounter As Long
Set doc = ActiveDocument
szFindTerm = InputBox("Enter the phrase you wish to find:")
lNumFfld = doc.FormFields.Count
lFfldCounter = 0
Set ffld = doc.FormFields(Selection.Bookmarks(1).Name)
If Len(szFindTerm) = 0 Then Exit Sub
Set rngStart = Selection.Range
Set rng = rngStart.Duplicate
rng.End = ffld.Range.End
szSearchString = rng.Text
Debug.Print szSearchString
lPos = InStr(ffld.Result, szFindTerm)
If lPos > 0 Then
Selection.MoveStart wdCharacter, lPos - 1
Selection.MoveEnd wdCharacter, Len(szFindTerm)
Exit Sub
End If
Do
Set ffld = ffld.Next
If ffld Is Nothing Then
Set ffld = ActiveDocument.FormFields(1)
End If
If rngStart.InRange(ffld.Range) Then
MsgBox "All form fields were searched; the phrase was not found."
Exit Sub
End If
ffld.Select
Selection.Collapse
Set rng = Selection.Range
rng.End = ffld.Range.End
szSearchString = rng.Text
Debug.Print szSearchString
lPos = InStr(ffld.Result, szFindTerm)
If lPos > 0 Then
Selection.MoveStart wdCharacter, lPos - 1
Selection.MoveEnd wdCharacter, Len(szFindTerm)
Exit Sub
End If
lFfldCounter = lFfldCounter + 1
Loop Until lFfldCounter = lNumFfld
End Sub
The Tools/Spelling command will function in unprotected sections of a document, but not in protected sections. In Word 2002 (but not in earlier versions) even the automatic spell check as you type will work in unprotected sections, and underline spelling mistakes with red, and grammatical mistakes with green.
You can spell check user the entire document by using a macro, however. The following macro is a very rudimentary one, that will spell check in a simple form document. You'll find a more sophisticated macro on the Word MVP website, that checks other areas than the main document, and only the form fields in the document.
Sub SpellCheckForm()
With ActiveDocument
If .ProtectionType <> wdNoProtection Then .Unprotect
.Range.LanguageID = wdEnglishUS
'''Include the following for 2000
'''.Range.NoProofing = False
If Options.CheckGrammarWithSpelling = True Then
.CheckGrammar
Else
.CheckSpelling
End If
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub
See the information for Word 97, 2000 and 2002 for general remarks. The macro code for running a spell check in Word 6/95:
Sub MAIN
DisableInput 1
ToolsUnprotectDocument
EditSelectAll
ToolsLanguage .Language = "English (UK)"
'Substitute the abbreviation for your language in the parentheses
StartOfDocument
ToolsSpelling
ToolsProtectDocument .NoReset = 1, .Type = 2
End Sub
Sometimes, you want to be able to turn form protection on and off without losing the user input. Here are a pair of macros that will allow you to do so.
Sub ToggleFormProtect()
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
MsgBox "The document has been protected"
Else
ActiveDocument.Unprotect
MsgBox "The document is now unprotected"
End If
End Sub
Sub MAIN
If DocumentProtection() Then
ToolsUnprotectDocument
Else
ToolsProtectDocument .NoReset = 1, .Type = 2
End If
End Sub
All form fields have a name, which is also a bookmark. You can see and change the name in the Form Field Options dialog box. By referencing this name, you can use the information in a text form field to perform calculations.
Create the calculation in a Text Form Field of the "calculation" type. Insert the formula in the "expression" box.
Expression: =Qty*price
Field name: Total
Note that a field of the "calculation" type is automatically disabled for user input.
Calculations can only be performed on numeric values, not on text.
Activate the "Calculate on Exit" checkbox in the Form Field Options dialog box for all fields included in a calculation (but not the calculation field, itself).
If Tools/Options/Update fields is activated in Tools/Options/Print you will see the results of calculated fields when you print out the form.
In order to see them update "on the fly", as you work, you need to use a very simple macro, which references the name of the calculation form field.
The macro must be assigned to the "Exit" event in the Form Field Options dialog box for each cell where you want the user to see an update after information has been entered. The "Exit" event takes place whenever a user leaves the form field either by pressing TAB or clicking in a different field with the mouse.
Sub Main
SetFormResult "Total"
End Sub
Each version of Word handles custom input in the Formatcombobox somewhat differently. Sometimes the formatting codes are accepted; more often than not they're ignored. Much more reliable is to add a numeric formatting switch after the formula in the Expression field. The following example shows how to format a result with two decimal places, and display nothing if the value is zero. A numeric formatting switch has three parts: positive result, negative result, and zero result. The two apostrophes (') before the closing quotes (") instruct Word to display a zero-length string (nothing).
=Price*Units \# "#.00,-#.00,''"
Note: The character that separates each part of the numeric switch depends on your system settings for the argument separator.
Note: This approach will not work with normal, textinput fields. In this case, only a macro that fires upon exiting the field, such as described in Knowledge Base article 212689 How to Create Ordinal (Legal) Dates in Form Fields will be effective.
Note: All fields in the form will update when any form field is exited where the "Calculate on Exit" option is activated.
Sub MAIN
duplInfo$ = GetFormResult$("OriginalFormFieldName")
SetFormResult "NewFormFieldName", duplInfo$
End Sub
The Knowledge Base article Q125892 explains that ampersands (&) are used in form drop-down lists as hot-key designators, so that the user can quickly jump to a list item using the keyboard. Therefore, ampersands will not be displayed in the list selection, nor will they be printed. Although the article cites all versions of Word, this holds true for Word 97, only. In Word 6.0 and 7.0 (95) the ampersands are displayed and printed.
Often, however, it would be necessary to display an ampersand in a company name, for example. One workaround to acheive this is to place a text box field directly adjacent to the drop-down list, with a macro assigned to the OnExit option of the list. The macro sets the text box to the drop-down list selection, then sets the drop-down selection to "empty" (a space, in the sample, the first item in the list). The text box field is not enabled for user input, so when the user clicks or tabs through this section, only the drop-down list is accessed.
Sub AssignDropDownValue()
With ActiveDocument
userSel = .FormFields("DropDown").Result
.FormFields("Text").Result = userSel
.FormFields("DropDown").DropDown.Value = 1
End With
End Sub
A Sample file (23 KB) is available for download. Warning: contains VBA macro code.
One can't assign a value to a Word form field checkbox directly. But sometimes one needs to sum up a value based on which checkboxes a user has activated. The example in the sample Word97/2000 document contained in ChkBx97.zip (12 KB) sums arbitrary values assigned to the checkboxes in a table row using a macro assigned to the Enter event of the checkboxes. The values are stored as part of the checkbox name.
By default, any and all checkboxes on a form can be checked. Sometimes, however, you'd like to be able to restrict the user to activating only one option of a group. This can be done using a macro. Notice that the checkboxes in the sample Word97/2000 document ( ChkOpt97.zip (8 KB) ) are contained in frames, and the frames have been assigned a bookmark. This bookmark is the key to the generic macro that can be used with any set of checkboxes on any form.
Option Explicit
Sub ActivateSingleCheckBox()
'Macro to Get Frame-Group Bookmark
'and current checkbox name
'passed to routine to select only
'current checkbox.
'Macro must be assigned to ENTER
'option of form field.
Dim docForm As Word.Document
Dim rngChkBoxGroup As Word.Range
Dim SelectedChkBox As String
Set docForm = ActiveDocument
'If group of Checkboxes are in a frame
'Frame's bookmark is recognized as first
'in the current selection
Set rngChkBoxGroup = Selection.Bookmarks(1).Range
'Gets the name of the current formfield
SelectedChkBox = Selection.FormFields(1).Name
EmptyOtherChkBoxes SelectedChkBox, rngChkBoxGroup
End Sub
Sub EmptyOtherChkBoxes(SelectedChkBox, rngChkBoxGroup)
'Macro to make sure only current form field
'checkbox in bookmarked group is activated.
Dim fld As Word.FormField
For Each fld In rngChkBoxGroup.FormFields
If fld.CheckBox.Valid = True Then
If fld.Name <> SelectedChkBox Then
fld.CheckBox.Value = False
Else
fld.CheckBox.Value = True
End If
End If
Next
End Sub
The same effect can be achieved in Word 6.0, but the macro solution is not as elegant. It is necessary to write a macro for each set of checkboxes and refer to each checkbox by name, as shown in the sample file ChkOpt60.zip (3 KB).
Sub MAIN
SelectedChkBox = SelInfo(30) + 1
Select Case BookmarkName$(SelectedChkBox)
Case "MSWord"
SetFormResult "MSWord", 1
SetFormResult "WordPro", 0
SetFormResult "WordPerfect", 0
Case "WordPro"
SetFormResult "MSWord", 0
SetFormResult "WordPro", 1
SetFormResult "WordPerfect", 0
Case "WordPerfect"
SetFormResult "MSWord", 0
SetFormResult "WordPro", 0
SetFormResult "WordPerfect", 1
Case Else
End Select
End Sub
In Word 6, pressing Enter moved to the next form field. In all later version of Word, from Word 7(95) through Word 2002, pressing Enter inserts a new paragraph (carriage return) in the form field. Only Tab moves between fields. It is possible to use a macro to intercept the action of the Enter key. Sample code for VBA-versions of Word can be found in KB Article Q187985.
In all versions of Word except Word 97, Enter and Exit macros work when navigating both with the keyboard and with the mouse.
Word 8.0 (97) was quite buggy in this respect. The macro only execute with mouse navigation if there is nothing typed in the field, or when using the keyboars (TAB) is used to move between the form fields. This is a bug described in Kowledge Base article 172488).
You can use ActiveX controls instead of Form Field controls in all Word97 and later documents protected for forms; you can also use them in unprotected documents. The controlling macro is stored as code within the document. It can be much more flexible, but the form is somewhat slower. (More details)
In order to insert an ActiveX control in a Word97 document you must display the Control ToolBox toolbar. ActiveX controls can be inserted as "floating objects" or "in-line". If you want the ActiveX field to behave the same way as a normal form field, you should insert it "in-line". Word 2000 and later do this automatically. In Word 97, position your cursor, hold Shift, then click the ActiveX control you want to use.
The document must be protected as a form (Tools/Protect document) in order to navigate between the form fields using the Tab key.
The presence of ActiveX controls in a document will trigger the macro security warning in Word 2000 and later, unless the document has been digitally signed and the signature trusted. If you open a form document, and the Control Toolbox appears, the Design mode is activate, you can't use the form fields, and can't unprotect the document, then your macro security setting (Tools/Macros/Security) is set to "High". You must set it to "Medium" then enable macros in order to use this form.
In order to write macro code for the control, click the "View Code" button in the Control Toolbox. The Visual Basic Editor will be displayed and you can select the type of action the control should react to (Got Focus = Enter; Lost Focus = Exit).
Click the "Properties" button in the Control Toolbox in order to change the control's name or the formatting.
When you're done, click the "Design Mode" button to return to working with your form document as a normal Word document. In order to make any changes to an ActiveX control, you must click this button again to re-enter Design Mode.
Note the uppercase "M" used for designating the month. This is important in order to distinguish from "m" for minutes.