How to respond the click event of submenu

I am writing a addin for RobotStudio, and I add a submenu under “Tools”. But I cannot respond the click event of it.

I tried to define a menu and a submenu as :

Public MyMenu As CommandBarPopup
Public WithEvents MySubMenu As CommandBarButton

But it returns an error message:“Object or class does not support the set of events”

:nauseated_face: Could you give me a hand?

Here is some simple code to start with

Dim WithEvents mRSE As RSE
Dim mToolsMenu As CommandBarPopup
Dim mMyMenu As CommandBarControl
Dim lngMyID As Long

Sub CreateMenu()
On Error Resume Next

Set mRSE = Application.RSE
Set mToolsMenu = mRSE.CommandBars(“Menu Bar”).Controls(“Tools”)
Set mMyMenu = mToolsMenu.Controls.Add(rsControlButton, “My Menu”)
lngMyID = mMyMenu.Id

End Sub

Private Sub mRSE_CommandBarControlClick(ByVal CommandID As Long, CancelDefault As Boolean)
If lngMyID = CommandID Then
MsgBox “You have clicked on my menu…”
End If
End Sub

If you wish to make your code a bit more readable then I wouldn’t save the ID but instead check it like this:

Dim WithEvents mRSE As RSE
Dim mToolsMenu As CommandBarPopup
Dim mMyMenu As CommandBarControl

Sub CreateMenu()
On Error Resume Next

Set mRSE = Application.RSE
Set mToolsMenu = mRSE.CommandBars(“Menu Bar”).Controls(“Tools”)
Set mMyMenu = mToolsMenu.Controls.Add(rsControlButton, “My Menu”)

End Sub

Private Sub mRSE_CommandBarControlClick(ByVal CommandID As Long, CancelDefault As Boolean)
If CommandID = Application.RSE.CommandBars(“Menu Bar”).Controls(“Tools”).Controls(“My Menu”).Id Then
MsgBox “You have clicked on my menu…”
End If
End Sub

But to make it language independent, I think using ID is better

:stuck_out_tongue_winking_eye: