Results 1 to 2 of 2
2002-05-17, 20:55 #1
- Join Date
- Apr 2002
- Stone Ridge, New York, USA
- Thanked 0 Times in 0 Posts
Calculating Checkbox form fields (Word 2000)
I need to add a column of checkboxes in a protected form. If the checkbox is checked, I need it to count as "1". Any ideas on how to do this? Thanks.
2002-05-22, 09:34 #2
- Join Date
- Mar 2002
- Thanked 29 Times in 29 Posts
Re: Calculating Checkbox form fields (Word 2000)
Assuming that the checkboxes are in a column in a table, and that you want to do something with the result in code, you can use the following function. It takes 2 arguments.
intTable = 1 for the first table in the document, 2 for the second table, etc.
intColumn = 1 for the first column in the table, 2 for the second column, etc.
Function CountCheckedBoxes(intTable As Integer, intColumn As Integer)
Dim intSum As Integer
Dim tbl As Table
Dim col As Column
Dim cel As Cell
Dim fld As FormField
On Error GoTo Exit_Function
intSum = 0
Set tbl = ActiveDocument.Tables(intTable)
Set col = tbl.Columns(intColumn)
For Each cel In col.Cells
For Each fld In cel.Range.FormFields
If fld.Type = wdFieldFormCheckBox Then
intSum = intSum - fld.CheckBox.Value
CountCheckedBoxes = intSum
Set fld = Nothing
Set cel = Nothing
Set col = Nothing
Set tbl = Nothing
The function uses the fact that a checked box has a value True = -1 and an unchecked box a value False = 0.
You can call this function as follows:
MsgBox CountCheckedBoxes(1, 2)
Selection.TypeText CountCheckedBoxes(1, 2)
This will display the number of checked boxes in the second column of the first table in the document.
If you want the number to be displayed in your document and to be updated immediately as the user checks/unchecks check boxes, things get more complicated. It might be better to embed an Excel table in the document in that case.