I am trying to create a custom class in Excel VBA to handle the events GotFocus and LostFocus for an OLEObject (ActiveX Control on a worksheet).
custom class clsSheetControl
Dim WithEvents objOLEControl as OLEObject
Public Sub Init(oleControl as OLEObject)
Set objOLEControl = oleControl
End Sub
end custom class
calling worksheet
Public Sub SetControlHandler()
set clsControl = new ClsSheetControl
clsControl.Init(Me.OLEObjects("cmdControl1")
End Sub
end worksheet
When I select the objOLEControl in the dropdown, I am able to create "GotFocus" and "LostFocus" in the custom class module, however when the line
Set objOLEControl = oleControl
is encountered in the custom class, I get the error
"459: Object or class does not support this set of events".
I tried searching for the answer but most of the results deal with accessing the control object within the OLEObject, not what I am trying to do here.
EDIT
This doesn't work on the worksheet either
Worksheet
Dim WithEvents objCtrl As OLEObject
Dim WithEvents chkCtrl As MSForms.CheckBox
Private Sub Worksheet_Activate()
Set chkCtrl = Me.OLEObjects("chkControl").Object
Set objCtrl = Me.OLEObjects("chkControl")
End Sub
Private Sub chkControl_GotFocus()
MsgBox ("chkControl has focus")
End Sub
The line
Set objCtrl = Me.OLEObjects("chkControl")
raises the same error. However accessing the GotFocus event directly (the chkControl_GotFocus event) is fine.