Consulting

Results 1 to 4 of 4

Thread: Counting checkboxes in a Word document

  1. #1

    Counting checkboxes in a Word document

    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
    Last edited by SamT; 05-15-2016 at 04:46 AM. Reason: Removed Text Formatting. Added CODE Tags with # Icon

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    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
    Last edited by El_muchacho; 05-15-2016 at 03:57 AM.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Please use the Thread Tools menu to mark your threads "Solved" when you are done.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •