Sep 14 2007
Using Word checkbox form fields as radio buttons
Based on an article on word.mvps.org here is an enhanced version of a routine to make Word checkbox formfields act as radio buttons when grouped by Frame or in a TableCell.
Notes:
- Assign routine to both Entry and Exit events
- Routine will work with both mouse and tab/spacebar. With tab, 2 boxes may simultaneously appear checked until you exit the selected checkbox.
- I trapped some kind of strange bug that occurred for me in Table cells. Details in the code.
Sub MakeCheckBoxesExclusive()
Dim oField As FormField
Dim oCheckedField As FormField
Dim bChecked As Boolean
Dim oContainer As Object
On Error GoTo errtrap
'\ Figure out if checkboxes are grouped by frame or table cell
If Selection.Frames.Count > 0 Then
Set oContainer = Selection.Frames(1)
ElseIf Selection.Cells.Count > 0 Then
Set oContainer = Selection.Cells(1)
End If
'\Get the value of the current checkbox
'\If it is checked then it will be the checked box
bChecked = Selection.FormFields(1).CheckBox.Value
If bChecked Then
Set oCheckedField = Selection.FormFields(1)
Else
'\Selected box is not checked
'\Look through other boxes for checked box
For Each oField In oContainer.Range.FormFields
If oField.CheckBox.Value = True Then
Set oCheckedField = oField
Exit For
next_field:
End If
Next oField
End If
'\Uncheck all the boxes
For Each oField In oContainer.Range.FormFields
oField.CheckBox.Value = False
Next oField
'\Check the designated box
If Not oCheckedField Is Nothing Then
oCheckedField.CheckBox.Value = True
End If
Exit Sub
errtrap:
'\ This error occurred when enumerating formfields in table cells
'\ Some fields enumerated as strings or numbers
If Err.Number = 4120 Then
Resume next_field
Else
Err.Raise Err.Number
End If
End Sub