Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it possible to automatically count cheked boxes in a form?

1,220 views
Skip to first unread message

TechPubs@discussions.microsoft.com dti TechPubs

unread,
Mar 23, 2006, 3:55:02 PM3/23/06
to
Using MS Word 2000 to set up test cases. We want to include form field check
boxes and have a tally at the end of each column to count how many boxes were
checked. Id there some way to write a macro to accomplish this (or soem other
way?)

Jezebel

unread,
Mar 23, 2006, 4:25:01 PM3/23/06
to
Easy to do with a macro, if you know how to write macros.


"dti TechPubs" <dti Tech...@discussions.microsoft.com> wrote in message
news:5C4B5472-666D-4742...@microsoft.com...

Graham Mayor

unread,
Mar 24, 2006, 9:57:35 AM3/24/06
to
Jezebel is a much better vba programmer than me, so I would have preferred
to have seen his solution, rather than his odd humour, however assuming that
your column of check boxes adopts the standard numbering and has check boxes
(say) 1 to 4 you can use a macro run from the last of those check boxes to
add the total checked to a DocVariable here called Count. You can add a
DocVariable field to the form to display that count. In order to update that
field you need to check the calculate on exit check box of that last check
box field.

The following is a sample of the macro required to total Check1 to Check4

Sub CheckedNo()
Dim Count, n, aVar, num As Integer
Count = 0
For n = 1 To 4 'number of check boxes
If ActiveDocument.FormFields("Check" & n).CheckBox.Value = True Then
Count = Count + 1
End If
Next n
For Each aVar In ActiveDocument.Variables
If aVar.Name = "Count" Then num = aVar.Index
Next aVar
If num = 0 Then
ActiveDocument.Variables.Add Name:="Count", Value:=Count
Else
ActiveDocument.Variables(num).Value = Count
End If
End Sub

If you have more than one set of boxes to total you will need more macros
and different variables. This one will total Check5 to Check8.

Sub CheckedNo2()
Dim Count2, n, aVar, num As Integer
Count2 = 0
For n = 5 To 8 'number of check boxes
If ActiveDocument.FormFields("Check" & n).CheckBox.Value = True Then
Count2 = Count2 + 1
End If
Next n
For Each aVar In ActiveDocument.Variables
If aVar.Name = "Count2" Then num = aVar.Index
Next aVar
If num = 0 Then
ActiveDocument.Variables.Add Name:="Count2", Value:=Count2
Else
ActiveDocument.Variables(num).Value = Count2
End If
End Sub

This will work until Jezebel shows us how it should be done ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org

Greg

unread,
Mar 24, 2006, 11:24:14 AM3/24/06
to
And Graham is better than me.

One problem with on exit macros is that the users may use the mouse
instead of tabbing out and firing the macro. Another approach might be
a final tally macro. To evaluate the specific columns one time before
printing or filing.

Say you have a three column table with data in column 1 and check boxes
in columns 2 and 3. You have a header row at and the last row will
hold the sums.

Somthing like this:

Sub FinalTally()
Dim oTbl As Word.Table
Dim i As Integer, j As Integer, k As Integer
Dim oLastRow As Integer
Set oTbl = ActiveDocument.Tables(1)
oLastRow = oTbl.Rows.Count
ActiveDocument.Unprotect
For j = 2 To 3
i = 0
For k = 2 To oLastRow - 1
If oTbl.Cell(k, j).Range.FormFields(1).CheckBox.Value = True Then
i = i + 1
End If
Next k
oTbl.Cell(oLastRow, j).Range.Text = i
Next j
ActiveDocument.Protect wdAllowOnlyFormFields, noReset:=True
End Sub

Jezebel

unread,
Mar 25, 2006, 12:44:04 AM3/25/06
to
Assuming you have checkboxes in columns (one per cell), and no other type of
formfields in the table, you could use ---

Dim pTable As Word.Table
Dim pCount() As Long
Dim pFormField As Word.FormField
Dim pColumn As Long

'Get a reference to the table we're working with
Set pTable = Selection.Tables(1)

'Dimension the array to store the column counts
ReDim pCount(1 To pTable.Columns.Count)

'Iterate the formfields in the table
For Each pFormField In pTable.Range.FormFields

'If checked, increment the count for the column containing this
formfield
If pFormField.CheckBox.Value Then
pColumn = pFormField.Range.Cells(1).ColumnIndex
pCount(pColumn) = pCount(pColumn) + 1
End If

Next

'Create/update the DocVariable for each column
For pColumn = 1 to ubound(pCount)
ActiveDocument.Variables("ColCount_" & pColumn) = pCount(pColumn)
Next

'Update fields to display the new counts
with ActiveDocument
if .ProtectionType <> wdNoProtection then
.Unprotect
end if
.Fields.Update
.Protect wdAllowOnlyFormFields, noReset:=True
end with


Use DocVariable fields to display the totals {DocVariable ColCount_1} etc.
If the table might contain other types of formfields, you'd need to check
the FormField.Type property. The DocVariable naming convention won't work if
you have more than one table that you want to run this on. You could use the
Table.ID property to set a unique value for each table, and include that in
the DocVariable names.


"Graham Mayor" <gma...@mvps.org> wrote in message
news:OyKyRP1T...@tk2msftngp13.phx.gbl...

0 new messages