I
Ian Baxter
I have a Word document, a form with various checkboxes and such, designed to
be a pre-qualification form for Contractors. At first it started with a need
to color the selected answer (of "Yes", "No" or "N/A") in blue. I designed a
macro to do this when the formfield is exited, and with strict naming
conventions for the formfields via bookmarks.
It was then decided that they would like the "form" to automatically "jump"
to a supplementary question if the N/A answer was ticked. Once completed, an
exit macro in the textfield is designed to take the user back to the field
after the "N/A" checkbox.
The code works so long as the user only uses the tab key to move from field
to field in the form. If the user decides to use a mouse and click on the
field after the N/A checkbox instead of using the tab key, Word crashes as
soon as the sub is exited. I am sure it is tied to the movement of the
insertion point via a .SELECT, but have been unable (after several rewrites
and various attempts at workarounds) to identify the cause and solution.
Code:
Public Sub Color_Check()
' This routine goes into the document and colors the checked value
' of either Yes, No, or NA as blue and the others as black
' if NA is chosen, a message is displayed and the user sent to
' the end of the document to fill out additional details
'
' All bookmarks are set as <main>_YES, <main>_NO, <main>_NA, <main>_Jump
and <main>_Back
'
' Initialize our internal values
'
Dim Name_Yes, Name_No, Name_NA As String
Dim IsNA, testVal As Boolean
Dim NameVal, TestName, SelectedNameVal, JumpBookMark, OldMacro As String
Dim BookPos As Range
Dim BookField As FormField
'
' So, only one form field will be selected, we get the value and name
' this way
SelectedNameVal = Selection.FormFields(1).Name
' We need to calculate the <main> value and the other permutations of the
' bookmark
'
NameVal = Left$(SelectedNameVal, InStr(SelectedNameVal, "_") - 1)
Name_Yes = NameVal & "_Yes"
Name_No = NameVal & "_No"
Name_NA = NameVal & "_NA"
'
' Now we determine if there is an NA option for this
' because it appears that there isn't always. We also set the bookmark to
' jump to if there is an "NA" value
'
IsNA = False
JumpBookMark = ""
If (ActiveDocument.Bookmarks.Exists(Name_NA)) Then
IsNA = True
JumpBookMark = NameVal & "_Jump"
End If
'
' And now we know the control that was called, we need to determine its
' state - if checked or not. Unchecking should simply turn it black.
'
' First - is it a checkbox?
If ActiveDocument.FormFields(SelectedNameVal).Type = wdFieldFormCheckBox
Then
testVal = ActiveDocument.FormFields(SelectedNameVal).CheckBox.Value
'
' If the document is protected, we need to unprotect it.
'
ActiveDocument.Unprotect
' If it was checked, then we have to color all the other ones black
and
' uncheck them
If testVal = True Then
' Which ones we check depends on the one we just exited
Select Case SelectedNameVal
Case Is = Name_Yes
ActiveDocument.Bookmarks(Name_No).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_No).CheckBox.Value = False
If IsNA Then
ActiveDocument.Bookmarks(Name_NA).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_NA).CheckBox.Value = False
End If
Case Is = Name_No
ActiveDocument.Bookmarks(Name_Yes).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_Yes).CheckBox.Value = False
If IsNA Then
ActiveDocument.Bookmarks(Name_NA).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_NA).CheckBox.Value = False
End If
Case Is = Name_NA
If ActiveDocument.Bookmarks.Exists(Name_Yes) Then
ActiveDocument.Bookmarks(Name_Yes).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_Yes).CheckBox.Value = False
End If
If ActiveDocument.Bookmarks.Exists(Name_No) Then
ActiveDocument.Bookmarks(Name_No).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_No).CheckBox.Value = False
End If
End Select
'
' Make sure we color the selected bookmark blue
Selection.Font.Color = wdColorBlue
Else
' Here we simply turn the checkbox black
ActiveDocument.Bookmarks(SelectedNameVal).Range.Font.Color =
wdColorBlack
End If
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
'
' We are forced to process this after the SELECT because
' we will end up selecting another area of text after we're
' done.
'
If (SelectedNameVal = Name_NA) And (testVal = True) Then
If (ActiveDocument.Bookmarks.Exists(JumpBookMark)) Then
MsgBox "Additional information is required. Jumping to that
section of the document.", vbOKOnly
ActiveDocument.Bookmarks(JumpBookMark).Select
End If
End If
End If
End Sub
be a pre-qualification form for Contractors. At first it started with a need
to color the selected answer (of "Yes", "No" or "N/A") in blue. I designed a
macro to do this when the formfield is exited, and with strict naming
conventions for the formfields via bookmarks.
It was then decided that they would like the "form" to automatically "jump"
to a supplementary question if the N/A answer was ticked. Once completed, an
exit macro in the textfield is designed to take the user back to the field
after the "N/A" checkbox.
The code works so long as the user only uses the tab key to move from field
to field in the form. If the user decides to use a mouse and click on the
field after the N/A checkbox instead of using the tab key, Word crashes as
soon as the sub is exited. I am sure it is tied to the movement of the
insertion point via a .SELECT, but have been unable (after several rewrites
and various attempts at workarounds) to identify the cause and solution.
Code:
Public Sub Color_Check()
' This routine goes into the document and colors the checked value
' of either Yes, No, or NA as blue and the others as black
' if NA is chosen, a message is displayed and the user sent to
' the end of the document to fill out additional details
'
' All bookmarks are set as <main>_YES, <main>_NO, <main>_NA, <main>_Jump
and <main>_Back
'
' Initialize our internal values
'
Dim Name_Yes, Name_No, Name_NA As String
Dim IsNA, testVal As Boolean
Dim NameVal, TestName, SelectedNameVal, JumpBookMark, OldMacro As String
Dim BookPos As Range
Dim BookField As FormField
'
' So, only one form field will be selected, we get the value and name
' this way
SelectedNameVal = Selection.FormFields(1).Name
' We need to calculate the <main> value and the other permutations of the
' bookmark
'
NameVal = Left$(SelectedNameVal, InStr(SelectedNameVal, "_") - 1)
Name_Yes = NameVal & "_Yes"
Name_No = NameVal & "_No"
Name_NA = NameVal & "_NA"
'
' Now we determine if there is an NA option for this
' because it appears that there isn't always. We also set the bookmark to
' jump to if there is an "NA" value
'
IsNA = False
JumpBookMark = ""
If (ActiveDocument.Bookmarks.Exists(Name_NA)) Then
IsNA = True
JumpBookMark = NameVal & "_Jump"
End If
'
' And now we know the control that was called, we need to determine its
' state - if checked or not. Unchecking should simply turn it black.
'
' First - is it a checkbox?
If ActiveDocument.FormFields(SelectedNameVal).Type = wdFieldFormCheckBox
Then
testVal = ActiveDocument.FormFields(SelectedNameVal).CheckBox.Value
'
' If the document is protected, we need to unprotect it.
'
ActiveDocument.Unprotect
' If it was checked, then we have to color all the other ones black
and
' uncheck them
If testVal = True Then
' Which ones we check depends on the one we just exited
Select Case SelectedNameVal
Case Is = Name_Yes
ActiveDocument.Bookmarks(Name_No).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_No).CheckBox.Value = False
If IsNA Then
ActiveDocument.Bookmarks(Name_NA).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_NA).CheckBox.Value = False
End If
Case Is = Name_No
ActiveDocument.Bookmarks(Name_Yes).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_Yes).CheckBox.Value = False
If IsNA Then
ActiveDocument.Bookmarks(Name_NA).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_NA).CheckBox.Value = False
End If
Case Is = Name_NA
If ActiveDocument.Bookmarks.Exists(Name_Yes) Then
ActiveDocument.Bookmarks(Name_Yes).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_Yes).CheckBox.Value = False
End If
If ActiveDocument.Bookmarks.Exists(Name_No) Then
ActiveDocument.Bookmarks(Name_No).Range.Font.Color =
wdColorBlack
ActiveDocument.FormFields(Name_No).CheckBox.Value = False
End If
End Select
'
' Make sure we color the selected bookmark blue
Selection.Font.Color = wdColorBlue
Else
' Here we simply turn the checkbox black
ActiveDocument.Bookmarks(SelectedNameVal).Range.Font.Color =
wdColorBlack
End If
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
'
' We are forced to process this after the SELECT because
' we will end up selecting another area of text after we're
' done.
'
If (SelectedNameVal = Name_NA) And (testVal = True) Then
If (ActiveDocument.Bookmarks.Exists(JumpBookMark)) Then
MsgBox "Additional information is required. Jumping to that
section of the document.", vbOKOnly
ActiveDocument.Bookmarks(JumpBookMark).Select
End If
End If
End If
End Sub