Option Explicit
Sub Tst()
EssaiAvecBouton "Essai"
End Sub
Sub EssaiAvecBouton(ByVal Titre As String)
Dim Msg As Object
Set Msg = CreateAvecBouton(Titre)
Msg.Show
DelPopupMsg Msg.Name
Unload Msg
Set Msg = Nothing
End Sub
Function CreateAvecBouton(ByVal Titre As String) As Object
Dim BarForm As Object, Lbl As Object, Btn As Object, X As Long
Application.VBE.MainWindow.Visible = False
Set BarForm = ThisWorkbook.VBProject.VBComponents.Add(3)
With BarForm
.Properties("Caption" ) = Titre
.Properties("Width" ) = 200
.Properties("Height" ) = 100
End With
Set Btn = BarForm.Designer.Controls.Add("forms.commandbutton.1" )
With Btn
.Caption = "Cliquer ici !..."
.Left = 60 .Top = 50
End With
With BarForm.CodeModule
X = .CountOfLines
.insertlines X + 1, "Sub CommandButton1_Click()"
.insertlines X + 2, " MsgBox ""coucou"""
.insertlines X + 3, " Me.Hide"
.insertlines X + 4, "End Sub"
End With
VBA.UserForms.Add (BarForm.Name)
Set CreateAvecBouton = UserForms(UserForms.Count - 1)
End Function
Sub DelPopupMsg(ByVal Nom As String)
With ThisWorkbook.VBProject.VBComponents
.Remove .Item(Nom)
End With
End Sub
|