PDA

View Full Version : [Excel 2010 VBA] Get events of an object created on a Frame, OLEObjects.



Artashes
06-15-2012, 01:47 AM
Hi All,

I am trying get the click events of a label ("lblCmdCancel") created on a frame, but unsuccessful. What am I doing wrong?

Here is the code:

Option Explicit


Private WithEvents lblCmdCancel As MSForms.Label


Private Sub CommandButton1_Click()
Dim frmMain As MSForms.Frame


Set frmMain = ActiveSheet.OLEObjects.Add(ClassType:="Forms.Frame.1").Object
With frmMain
.Caption = ""
.BorderStyle = fmBorderStyleSingle
.BorderColor = &H80000006
.Width = 164
.Height = 128


' Some code here ....


Set lblCmdCancel = .Controls.Add("Forms.Label.1", "lblCmdCancel", True)
With lblCmdCancel
.BackColor = &H80000000
.BorderStyle = fmBorderStyleSingle
.Left = 84
.Top = 102
.Width = 72
.Caption = "Cancel"
.TextAlign = fmTextAlignCenter
MsgBox .Name
End With
End With
End Sub


Private Sub lblCmdCancel_Click()
MsgBox "Clicked!"
End Sub

CodeNinja
06-15-2012, 06:27 AM
Remove the top line of:
Private withevents lblCmdCancel as msforms.label

Because labels have events, you do not need to overwrite their events and I believe that is what you are doing...