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