PDA

View Full Version : Create Object



zirus
12-05-2006, 07:40 AM
Hi
Can a control be created/designed & Named @ runtime or must it be done when designing your application via vbe manually....

if so.... please give me a simple syntax eg.......i'm sure i'l be able to navigate my way from there.

thanx in advance

Bob Phillips
12-05-2006, 10:44 AM
'-----------------------------------------------------------------
Sub CreateControlButton()
'-----------------------------------------------------------------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Left:=200, Top:=100, Width:=80, Height:=32)

'To set with a cell
'With Range("H2")
' Set oOLE = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
' Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
'End With

With oOLE
.Object.Caption = "Run myMacro"
.Name = "myMacro"
End With

With ThisWorkbook.VBProject.VBComponents(oWs.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", oOLE.Name) + 1, _
vbTab & "If Range(""A1"").Value > 0 Then " & vbCrLf & _
vbTab & vbTab & "Msgbox ""Hi""" & vbCrLf & _
vbTab & "End If"

End With

End Sub

tstom
12-10-2006, 06:38 AM
If in a userform class, simply type Controls.Add. Select Add and hit F1 for help. The help files provide several examples, program ids, ect...