Consulting

Results 1 to 6 of 6

Thread: Ignoring an inexistant element in a loop

  1. #1

    Ignoring an inexistant element in a loop

    Hello, i have another problem with my function.
    This function is made to check if a checkbox is checked and to hide the associated text (placed in a bookmark) if i isn't.
    In any case the checkbox (without the associated text) is hidden.

    Here is the complete function
    Private Sub CommandButton21_Click()With ActiveDocument




    Dim intI As Integer
    intI = 1
    Dim Str As String
    Str = "CaseACocher"
    Dim Str2 As String
    Dim Str3 As String


    Dim fldCheck As FormField
    Dim Compteur1 As Integer
    Dim Compteur2 As Integer


    For Each fldCheck In ActiveDocument.FormFields
    If fldCheck.Type = wdFieldFormCheckBox Then
    fldCheck.Range.Font.Hidden = True
    Compteur1 = Compteur1 + 1

    End If
    Next fldCheck




    For intI = 1 To Compteur1 Step 1
    Str2 = Str & intI
    If .FormFields(Str2).CheckBox.Value = False Then
    .Bookmarks(Str2).Range.Font.Hidden = True
    Else


    End If
    Next intI


    .Protect wdAllowOnlyFormFields, noreset


    End With
    End Sub




    The problem is in this loop

    For intI = 1 To Compteur1 Step 1Str2 = Str & intI
    If .FormFields(Str2).CheckBox.Value = False Then
    .Bookmarks(Str2).Range.Font.Hidden = True
    Else


    End If
    Next intI
    The problem is that the whole function only runs if we have conitnuous variable name which are CaseACocher1 then CaseACocher2 then CaseACocher3 CaseACocher4 etc.

    If i a user decides to change the Word document, to delete checkboxes, the loop will be stopped because a bookmark won't be find because i has been deleted.
    For example if we have : bookmark1 then bookmark2 then bookmark4 the loop won't find bookmark3 and stop.

    So can i add something to ignore if something doesn't exist ?
    Or if there is something to directly get each bookmarks associated with the checkboxes (by default a bookmark is associated with a checkbox, if i use their name it must run), so i won't need a counter by just a "ForEachCheckbox.bookmark" (or something like that).

    I hope it was clear enough.
    Thanks

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Please use the # icon for CODE Tags

    Try this
    For intI = 1 To Compteur1 Step 1
    Str2 = Str & intI
    If Not .FormFields(Str2) is Nothing then
    If .FormFields(Str2).CheckBox.Value = False Then _
    .Bookmarks(Str2).Range.Font.Hidden = True
    End If
    Next intI
    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

  3. #3
    I still have a problem with this line : If Not .FormFields(Str2) Is Nothing Then

    Private Sub CommandButton21_Click()With ActiveDocument
    
    
    
    
    Dim intI As Integer
    intI = 1
    Dim Str As String
    Str = "CaseACocher"
    Dim Str2 As String
    Dim Str3 As String
    
    
    Dim fldCheck As FormField
        Dim Compteur1 As Integer
        Dim Compteur2 As Integer
    
    
    
    
    For intI = 1 To 500 Step 1
     Str2 = Str & intI
        If Not .FormFields(Str2) Is Nothing Then
            If .FormFields(Str2).CheckBox.Value = False Then _
            .Bookmarks(Str2).Range.Font.Hidden = True
        End If
    Next intI
    
    
    
    
    For Each fldCheck In ActiveDocument.FormFields
            If fldCheck.Type = wdFieldFormCheckBox Then
                fldCheck.Range.Font.Hidden = True
              
            End If
        Next fldCheck
    
    
    
    
    .Protect wdAllowOnlyFormFields, noreset
    
    
    End With
    End Sub


  4. #4
    Presumably this relates to your other thread. In order to explore the best way to handle this, how is the associated text associated with the check box? How are the bookmarks of the check box and the trext related? Can you post the document?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    I can't join my .dotm document so i joined a screenshot.
    It works, but if a user decides to change something directly in the word document for example, to delete a proposition, the associated bookmark will dissapear, so if the proposition 2 is deleted, the loop will stop because the bookmark won't be found.
    The loop goes from 1 to 500, so the bookmark500 won't be find too, but it is necessary.
    Attached Images Attached Images

  6. #6
    If the illustration is an indication of the issue you need
    Dim fldCheck As FormField
        For Each fldCheck In ActiveDocument.FormFields
            If fldCheck.Type = wdFieldFormCheckBox Then
                If fldCheck.CheckBox.Value = False Then
                    fldCheck.Range.Paragraphs(1).Range.Font.Hidden = True
                End If
            End If
        Next fldCheck
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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