Group: microsoft.public.word.vba.general
From: "Doug Robbins - Word MVP"
Date: Thursday, February 21, 2008 5:22 PM
Subject: Re: Checkbox calculations in form

Easiest way is the run a macro with the following code, adjusting the table
index and number of the cell as required

Dim i as Long, result as Long
result = 0
With ActiveDocument
With .Tables(1)
For i = 1 to 15
If .Cell(i, 1).Range.FormFields(1).CheckBox.Value = True Then
result = result + 1
End if
Next i
End With
.Formfields("Text108").Result = result
End With

--
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

"Jamie" wrote in message
news:17D378C8-FC43-446D-B822-E8B9D2C0E221@microsoft.com...
> Hi Jay, I am trying to use the coding you supplied to JC, but I'm not sure
> how to tweek it to fit my scenario.
>
> I'm working in Word 2003, I have a table set up (15 rows). I have a
> column
> titled Intake. The fields in this column are check boxes (1 for each
> row).
> I have given the check boxes in this column a new bookmark name, Intake
> (e.g., Intake1, Intake2 all the way to Intake15). Like JC, I just want to
> count how many checkboxes have been checked. I do not need to calculate a
> percentage as JC did. My coding is as follows:
>
> Sub IntakeCount()
> Dim TotalCheckBoxes as Long
> Dim CheckedBoxes as Long
> Dim FFld as FormField
> Dim myDoc as Document
>
> Set myDoc = ActiveDocument
>
> For i = 1 To TotalCheckBoxes
> Set FFld = myDoc.FormFields("Intake" & i)
> If FFld.CheckBox.Value = True Then
> CheckedBoxes = CheckedBoxes + 1
> End If
> Next i
>
> I have the macro run On Entry in formfield, Text108. Nothing happens. I
> can't figure out what I'm missing. Any help you can give me would be
> wonderful! Thanks
>
> --
> Jamie
>
>
> "Jay Freedman" wrote:
>
>> On Fri, 1 Feb 2008 11:40:41 -0800, J C
>> wrote:
>>
>> >Hi Jay, I should've have been a bit more specific. This form has
>> >multiple
>> >sections with different field types (including checkboxes), and for each
>> >section is a "Completed" checkbox for that section. So I wouldn't want
>> >this
>> >script to count all of the checkboxes but only these "Completed"
>> >checkboxes.
>> >How can this script be modified to include only the section "Completed"
>> >checkboxes? If there is a better way of doing so I am all ears.
>>
>> This can be done, but you have to tell the macro how to recognize which
>> ones are
>> the "Completed" checkboxes. Do they have something specific in their
>> names that
>> aren't in any other checkbox's name? Or are they formatted with a
>> specific style
>> that isn't used anywhere else? Or some other unique characteristic that a
>> macro
>> could use to say "this one should be counted, but that one shouldn't"?
>>
>> If you don't have any such characteristic yet, probably the easiest one
>> is to
>> change the names of the "Completed" boxes. When the form is unprotected,
>> double-click one of these checkboxes to open its Options dialog and
>> change the
>> entry in the "Bookmark" box. For example, you could name them
>> "Completed1",
>> "Completed2", etc. where the number is the section number.
>>
>> Then the For loop in the macro can be rewritten like this:
>>
>> For i = 1 To TotalCheckBoxes
>> Set FFld = myDoc.FormFields("Completed" & i)
>> If FFld.CheckBox.Value = True Then
>> CheckedBoxes = CheckedBoxes + 1
>> End If
>> Next i
>>
>> This can be rather touchy -- if the value of TotalCheckBoxes is wrong, or
>> if you
>> don't have a checkbox name for each value of i between 1 and
>> TotalCheckBoxes,
>> then an error will stop the macro. It's possible to add error checking to
>> catch
>> these problems; I didn't show it here to keep things simple.
>>
>> Another approach is to use the For Each as in the original macro, and use
>> a
>> different If statement in place of "If FFld.Type = wdFieldFormCheckBox
>> Then" to
>> make sure the name is "Completed" and a number:
>>
>> If FFld.Name Like "Completed#" Then
>>
>> Make the same replacement in the CheckboxCount function if you use that.
>>
>> --
>> Regards,
>> Jay Freedman
>> Microsoft Word MVP FAQ: http://word.mvps.org
>> Email cannot be acknowledged; please post all follow-ups to the newsgroup
>> so all may benefit.
>>


Safety Articles | Usenet Groups | Usenet News | Bluegrass