Consulting

Results 1 to 2 of 2

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

  1. #1

    Question [Excel 2010 VBA] Get events of an object created on a Frame, OLEObjects.

    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:
    [VBA]
    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
    [/VBA]

  2. #2
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •