Bonjour,
Avec votre aide, j'ai réussi à créer, Ici, un bouton activex.
Maintenant, je dois lancer une macro à l'appui du bouton.
j'ai donc ajouter ces lignes à la création de mon bouton:
Cela ne fonctionne pas.
Code : 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 Sub CreerBouton() Dim LeBouton As OLEObject Dim Code As String Dim NextLine As Long With ActiveSheet Set LeBouton = .OLEObjects.Add(ClassType:="Forms.CommandButton.1", _ Left:=17, Top:=1, Height:=32, Width:=100) End With LeBouton.Object.Caption = "Test" ' Ajouter le code du bouton Code = "Sub test()" & vbCrLf Code = Code & "msgbox ""TEST""" & vbCrLf Code = Code & "End Sub" With ThisWorkbook.VBProject.VBComponents(ActiveSheet.Name).CodeModule NextLine = .CountOfLines + 1 .InsertLines NextLine, Code End With End Sub
EDIT: J'ai trouvé.
Il faut dans le ruban > onglet développeur > Sécurité des macros Cocher l'option "Accès approuvé au modèle d'objet du projet VBA"
Puis, modifier le code comme suit:
Voila, en espérant que cela aide quelqu'un...
Code : 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 Sub CreerBouton() Dim monBouton As OLEObject Dim Code As String Dim NextLine As Long Dim nom As String With ActiveSheet Set monBouton = .OLEObjects.Add(ClassType:="Forms.CommandButton.1", _ Left:=17, Top:=1, Height:=32, Width:=100) End With monBouton.Object.Caption = "Test" ' Ajouter le code du bouton nom = monBouton.Name Code = "Private Sub " & nom & "_Click()" & vbCrLf Code = Code & "msgbox ""TEST""" & vbCrLf Code = Code & "End Sub" With ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule NextLine = .CountOfLines + 1 .InsertLines NextLine, Code End With End Sub
Partager