troubleshooting Question

How does OnAction property works ??

Avatar of c_sh
c_shFlag for India asked on
Visual Basic Classic
18 Comments1 Solution463 ViewsLast Modified:

I am designing a Addin for outlook that has a custom menuitem suppose, "MyMenuItem". I have added this new menu item and capturing the events by getting the reference of the new menuitem in a global variable "fMenuItem" declared as
Dim WithEvents fMenuItem As Office.CommandBarButton

Everything works fine if I am having a single window opened in an Outlook session. Now the problem arrises when new windows are opened, for example by double clicking the selected e-mail. At this point, a new instance of menu is created and keeping the reference of new one will loose the old refernce.

To sort out this I tried using OnAction property of CommandBarControl object. But unfortunatrly this is not working, I mean the function/sub specified for OnAction is not being invoked by clicking the menu item.

Can anybody help me sorting out this problem?

I have following code:

Dim cbCtrl As CommandBarControl  
Dim cmdControl As CommandBarControl
Dim cbBtn As Office.CommandBarButton  


    ' add stop MyMenuItem
    With cmdControl
      Set cbCtrl = .Controls.Add(Type:=msoControlButton, Temporary:=True, Before:=2)
      Set cbBtn = cbCtrl

      With cbBtn
        .Style = msoButtonIconAndCaption
        .FaceId = 228
      End With

      With cbCtrl
         .Caption = "MyMenuItem"
         .Tag = .Caption
         .ToolTipText = "This is MyMenuItem :-) "
         .OnAction = "runme"
      End With
   End With

Sub runme()
    MsgBox " Run me called "
End Sub


In summary the question is:
- I have menu Item Tools->MyMenuItem.
- This menu item is added to all the newly opened windows from withen the outlook session.
- I have to capture events from all the "MyMenuItem"
- How can this task be done?

Waiting for suggestions...


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 18 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 18 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros