Option Explicit
Sub AddControls()
Dim Frm As Object
Dim Btn As MSForms.CommandButton
Dim x As Long
Dim n As Long
Dim BtnName As String
For x = 1 To ThisWorkbook.VBProject.VBComponents.Count
If ThisWorkbook.VBProject.VBComponents(x).Type = 3 Then
Set Frm = ThisWorkbook.VBProject.VBComponents(x)
Set Btn = Frm.Designer.Controls.Add("forms.CommandButton.1")
With Btn
.Caption = "Caption"
.Height = 25
.Width = 60
.Left = 12
.Top = 6
End With
With ThisWorkbook.VBProject.VBComponents(x).CodeModule
n = .CountOfLines
.InsertLines n + 1, "Sub CommandButton1_Click()"
.InsertLines n + 2, vbNewLine
.InsertLines n + 3, vbTab & "MsgBox " & """" & "Hi" & """"
.InsertLines n + 4, vbNewLine
.InsertLines n + 5, "End Sub"
End With
End If
Next x
End Sub
|