Initialisation objet checkbox ActiveX Word.
Bonjour,
Je dispose de plusieurs dizaines de CheckBox ActiveX dans un document Word et je souhaite cocher certaines de ces cases depuis une procédure VBA executée depuis excel.
Pour tester, les quelques lignes de codes exécutées dans un module Excel fonctionnent parfaitement :
Code:
1 2 3 4 5 6 7 8 9 10
| For i = 1 To 5
If tab_bruit(1, i) Then
wordDoc.CB_Infr_rout.Value = True
If i = 1 Then wordDoc.CB_Cat1.Value = True
If i = 2 Then wordDoc.CB_Cat2.Value = True
If i = 3 Then wordDoc.CB_Cat3.Value = True
If i = 4 Then wordDoc.CB_Cat4.Value = True
If i = 5 Then wordDoc.CB_Cat5.Value = True
End If
Next i |
Ayant pas mal d'éléments, je souhaiterais pouvoir initialiser un objet d'après le nom de la variable. Malgré pas mal de recherche je ne trouve pas de solution. Dans l'idéal, ça pourrait ressembler à ça :
Code:
1 2 3 4 5 6 7 8 9
| For i = 1 To 5
If tab_bruit(1, i) Then
wordDoc.CB_Infr_rout.Value = True
Set chBx = checkbox("CB_Cat" & i) ' ???
wordDoc.chBx.Value = True
End If
Next i |
J'ai déjà cherché du côté de word avec une solution de ce type (qui ne marche pas lorsque l'appel se fait depuis Excel...) :
APPEL DE LA FONCTION :
Code:
1 2 3 4 5 6 7 8 9
| For i = 1 To 5
If tab_bruit(1, i) Then
wordDoc.CB_Infr_rout.Value = True
Set obj = getControl("CB_cat" & i, wordDoc)
If Not obj Is Nothing Then
obj.Object.Value = True
End If
End If
Next i |
FONCTION :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Function getControl(name As String, doc As Document) As OLEFormat
Dim ish As InlineShape
For Each ish In doc.InlineShapes
If ish.Type = wdInlineShapeOLEControlObject Then
If ish.OLEFormat.Object.name = name Then
Set getControl = ish.OLEFormat
Exit Function
End If
End If
Next
Set getControl = Nothing
End Function |
"Erreur d’exécution 13 : incompatibilité de type" à la ligne 8...
En espérant avoir été clair, merci d'avance pour vos conseils.