PDA

View Full Version : Ignoring an inexistant element in a loop



El_muchacho
05-15-2016, 05:11 AM
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

SamT
05-15-2016, 05:19 AM
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

El_muchacho
05-15-2016, 06:00 AM
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

gmayor
05-15-2016, 08:45 PM
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?

El_muchacho
05-16-2016, 03:03 AM
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.

gmayor
05-16-2016, 05:35 AM
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