Combining Checkboxes in Word Forms

A

Andrew Glennie

Hi All,

I have a Word template on which there is a group of 3 checkboxes. I would
like to prevent users from checking more than one of the group (like an
option group). I thought this code might work, but it does only part of the
time...

If ActiveDocument.FormFields("SectionE1").CheckBox.Value = True Then
ActiveDocument.FormFields("SectionE2").CheckBox.Value = False
ActiveDocument.FormFields("SectionE3").CheckBox.Value = False
ElseIf ActiveDocument.FormFields("SectionE2").CheckBox.Value = True Then
ActiveDocument.FormFields("SectionE1").CheckBox.Value = False
ActiveDocument.FormFields("SectionE3").CheckBox.Value = False
ElseIf ActiveDocument.FormFields("SectionE3").CheckBox.Value = True Then
ActiveDocument.FormFields("SectionE1").CheckBox.Value = False
ActiveDocument.FormFields("SectionE2").CheckBox.Value = False
End If

Any ideas how I might make this work?

Regards

Andrew
 
D

Doug Robbins - Word MVP

In this method, while it is necessary to use the mouse to check the required
box, it is then possible to use the tab key to move out of the box without
the value of the box that gets the focus being set to true

For it to work, the check boxes in each group of checkboxes that are to be
mutually exclusive must have bookmark names of the form

Chk####$

where

#### is a four digit number that is the same for each checkbox in the group
(to distinguish the checkboxes in that group from those in any other group
that must have a different four digit number assigned to them.

$ is an Alpha character A, B, C, D, E that is used to distinguish between
the individual checkboxes in each group.

For example, use the bookmark names:

chk1234A, Chk1234B and Chk1234C

The following macro is run on entry to each of the checkboxes:

Public Sub ExclusiveCheckBoxes()
Dim ChkCode As String
Dim ChkStart As String
Dim frmFld As FormField
Dim LtrStr As String
Dim NextChk As String
Dim tmpFrmFld As FormField

LtrStr = "ABCDE"

Set frmFld = Selection.FormFields(1)

' Did something happen?
If frmFld.Result > 0 Then
ChkName = frmFld.Name
If UCase(Left(ChkName, 3)) = "CHK" Then
ChkStart = Left(ChkName, 7): ChkCode = UCase(Right(ChkName, 1))
Select Case ChkCode
Case "A", "B", "C", "D", "E"
' Find all the other members of this group and clear them - yes
this will also clear the selected checkbox
For i = 1 To Len(LtrStr)
NextChk = ChkStart & Mid(LtrStr, i, 1)
If ActiveDocument.Bookmarks.Exists(NextChk) Then
Set tmpFrmFld = ActiveDocument.FormFields(NextChk)
tmpFrmFld.CheckBox.Value = False
End If
Next i
' Set the check box that was selected
frmFld.CheckBox.Value = True
Case Else
End Select
End If
End If

End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
A

Andrew Glennie

Thanks Doug.

Much appreciated

Regards

Andrew

Doug Robbins - Word MVP said:
In this method, while it is necessary to use the mouse to check the required
box, it is then possible to use the tab key to move out of the box without
the value of the box that gets the focus being set to true

For it to work, the check boxes in each group of checkboxes that are to be
mutually exclusive must have bookmark names of the form

Chk####$

where

#### is a four digit number that is the same for each checkbox in the group
(to distinguish the checkboxes in that group from those in any other group
that must have a different four digit number assigned to them.

$ is an Alpha character A, B, C, D, E that is used to distinguish between
the individual checkboxes in each group.

For example, use the bookmark names:

chk1234A, Chk1234B and Chk1234C

The following macro is run on entry to each of the checkboxes:

Public Sub ExclusiveCheckBoxes()
Dim ChkCode As String
Dim ChkStart As String
Dim frmFld As FormField
Dim LtrStr As String
Dim NextChk As String
Dim tmpFrmFld As FormField

LtrStr = "ABCDE"

Set frmFld = Selection.FormFields(1)

' Did something happen?
If frmFld.Result > 0 Then
ChkName = frmFld.Name
If UCase(Left(ChkName, 3)) = "CHK" Then
ChkStart = Left(ChkName, 7): ChkCode = UCase(Right(ChkName, 1))
Select Case ChkCode
Case "A", "B", "C", "D", "E"
' Find all the other members of this group and clear them - yes
this will also clear the selected checkbox
For i = 1 To Len(LtrStr)
NextChk = ChkStart & Mid(LtrStr, i, 1)
If ActiveDocument.Bookmarks.Exists(NextChk) Then
Set tmpFrmFld = ActiveDocument.FormFields(NextChk)
tmpFrmFld.CheckBox.Value = False
End If
Next i
' Set the check box that was selected
frmFld.CheckBox.Value = True
Case Else
End Select
End If
End If

End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top