Bonjour à tous,
Je suis BigZouk, débutant en vba et voici mon problème:
J’ai un certain nombre (exemple 3) de ComboBox (nommés ComboBox1 à ComboBox3 afin de faciliter la gestion par macro) dans une section de mon document Word.
Dans un premier temps, je voudrais les remplir. Pour cela, j’ai écrit le code suivant (à peut-être améliorer) :
Code Vba : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 Private Sub ComboBox_Section1() If ActiveDocument.ComboBox1.ListCount = 0 Then With ActiveDocument.ComboBox1() .AddItem "" .AddItem "texte 1.1" .AddItem "texte 1.2" .AddItem "texte 1.3" .Locked = False End With End If If ActiveDocument.ComboBox2.ListCount = 0 Then With ActiveDocument.ComboBox2() .AddItem "" .AddItem "texte 2.1" .AddItem "texte 2.2" .AddItem "texte 2.3" .Locked = False End With End If If ActiveDocument.ComboBox3.ListCount = 0 Then With ActiveDocument.ComboBox3() .AddItem "" .AddItem "texte 3.1" .AddItem "texte 3.2" .AddItem "texte 3.3" .Locked = False End With End If End Sub
Dans un deuxième temps, je souhaite les vider de leur liste afin de pouvoir injecter d’autres éléments.
Pour l’instant j’ai réussi à écrire le premier code ci-dessous (que je vais affecter à un CommandButton1 plus tard)
Code Vba : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 Private Sub CommandeB_Section1() Dim j As Integer, k As Integer For j = 1 To ActiveDocument.ComboBox1().ListCount If ActiveDocument.ComboBox1().ListCount >= 1 Then If ActiveDocument.ComboBox1().ListIndex = -1 Then ActiveDocument.ComboBox1().ListIndex = ActiveDocument.ComboBox1().ListCount - 1 End If End If ActiveDocument.ComboBox1.RemoveItem (ActiveDocument.ComboBox1().ListIndex) Next j For j = 1 To ActiveDocument.ComboBox2().ListCount If ActiveDocument.ComboBox2().ListCount >= 1 Then If ActiveDocument.ComboBox2().ListIndex = -1 Then ActiveDocument.ComboBox2().ListIndex = ActiveDocument.ComboBox2().ListCount - 1 End If End If ActiveDocument.ComboBox2.RemoveItem (ActiveDocument.ComboBox2().ListIndex) Next j For j = 1 To ActiveDocument.ComboBox3().ListCount If ActiveDocument.ComboBox3().ListCount >= 1 Then If ActiveDocument.ComboBox3().ListIndex = -1 Then ActiveDocument.ComboBox3().ListIndex = ActiveDocument.ComboBox3().ListCount - 1 End If End If ActiveDocument.ComboBox3.RemoveItem (ActiveDocument.ComboBox3().ListIndex) Next j End Sub
Afin d’optimiser le code ci-dessus, j’ai essayé d’utiliser des boucles « for next » en concaténant la variable d’une boucle (voir code ci-dessous) mais je n’ai que des erreurs malgré plusieurs tentatives.
Code Vba : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 Private Sub CommandeB_Section2() Dim i As Integer, j As Integer, k As String, l As String, m As String, n As String, LaCommande0 As Variant, LaCommande1 As Object, LaCommande2 As Object For i = 1 To 8 k = "ActiveDocument.ComboBox" & i & "().ListCount" l = "ActiveDocument.ComboBox" & i & "().ListIndex" LaCommande0 = Replace(k, """", "") For j = 1 To LaCommande0 If LaCommande0 >= 1 Then If Replace(l, """", "") = -1 Then m = l & " = " & l & "-1" LaCommande1 = Replace(m, """", "") LaCommande1 End If End If n = "ActiveDocument.ComboBox" & i & ".RemoveItem(ActiveDocument.ComboBox" & i & "().ListIndex)" LaCommande2 = Replace(n, """", "") LaCommande2 Next j Next i End Sub
Par ailleurs, je voudrais savoir comment faire référence (faire appel dans une procédure) à toute la collection « InlineShapes » d’une section donnée de mon document.
Voilà mon problème qui est probablement simple à régler. Je vous remercie.
Partager