PDA

View Full Version : [SOLVED:] Counting checkboxes in a Word document



El_muchacho
05-14-2016, 05:03 PM
Hello, i want to count the checkboxes in my Word document, i wrote something but there is something wrong with the counter.
After clicking the button, the function checks the checkboxes and only displays those which have been checked.
Thanks


Private Sub CommandButton1_Click()
With ActiveDocument


Dim intI As Integer
intI = 1

Dim Str As String

Str = "CaseACocher"

Dim Str2 As String


Dim Compteur As Integer
Compteur = 0

For Each fldCheck In ActiveDocument.FormFields
If fldCheck.Type = CheckBox Then

Compteur = Compteur + 1
End If
Next fldCheck



For intI = 1 To Compteur Step 1

Str2 = Str & intI

If .FormFields(Str2).CheckBox.Value = False Then
.Bookmarks(Str2).Range.Font.Hidden = True

Else

.Bookmarks(Str2).Range.Font.Hidden = False

End If

Next intI

.Protect wdAllowOnlyFormFields, noreset



End With
End Sub

gmayor
05-14-2016, 08:44 PM
I think the following is what you are looking for:

Option Explicit
Private Sub CommandButton1_Click()
Dim fldCheck As FormField
Dim Compteur1 As Integer
Dim Compteur2 As Integer

For Each fldCheck In ActiveDocument.FormFields
If fldCheck.Type = wdFieldFormCheckBox Then
Compteur1 = Compteur1 + 1
If fldCheck.CheckBox.Value = False Then
fldCheck.Range.Font.Hidden = True
Compteur2 = Compteur2 + 1
End If
End If
Next fldCheck
MsgBox "There were " & Compteur1 & " checkboxes." & vbCr _
& Compteur2 & " were hidden."
lbl_Exit:
Set fldCheck = Nothing
Exit Sub
End Sub

El_muchacho
05-15-2016, 03:27 AM
Thank you for your answer, the part i need is in it. The function not only displays
MsgBox "There were " & Compteur1 & " checkboxes." & vbCr _
& Compteur2 & " were hidden.", i needed the number of checkboxes to set the limit of my for loop, which tests and hides checkboxes and associated text. If i just put the limit to 1000, it won't run because there is no CaseACocher1000.
Then in any cases i needed to hide checkboxes (without associated text).
Tip top

SamT
05-15-2016, 04:48 AM
Please use the Thread Tools menu to mark your threads "Solved" when you are done.