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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| Option Explicit
' Références à cocher
' Microsoft Visual Basic for Applications Extensibility 5.3
' Microsoft Forms 2.0 Object Library
Sub UserForm_Dynamique()
Dim Usf As Object
Dim Lbl As MSForms.Label
Dim CB As MSForms.CommandButton
Dim sStr As String
Dim i As Long
Set Usf = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
With Usf
.Properties("Caption") = "UserForm Dynamique"
.Properties("Height") = 240
.Properties("Width") = 320
End With
Set CB = Usf.Designer.Controls.Add("forms.CommandButton.1")
With CB
.Caption = "Fermer"
.Left = 200
.Top = 180
End With
Set Lbl = Usf.Designer.Controls.Add("forms.Label.1")
With Lbl
.Caption = "mon texte"
.TextAlign = fmTextAlignCenter
.Left = 20
.Top = 20
.BackColor = vbRed
.BorderStyle = fmBorderStyleSingle
End With
sStr = "Sub CommandButton1_Click()" & _
vbCrLf & "Unload Me" & _
vbCrLf & "End Sub"
With Usf.CodeModule
i = .CountOfLines
.InsertLines i + 1, sStr
End With
sStr = "Sub Label1_Click()" & _
vbCrLf & "MsgBox " & _
"""Vous avez cliqué sur mon texte""" & _
vbCrLf & "End Sub"
With Usf.CodeModule
i = .CountOfLines
.InsertLines i + 1, sStr
End With
VBA.UserForms.Add(Usf.Name).Show
ThisWorkbook.VBProject.VBComponents.Remove Usf
End Sub |
Partager