More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 16-Jul-2018 06:57

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Outlook and .NET Printer Friendly Version
Title: Add submenus to context menu (Outlook 2007)
Description: This sample for VS 2008 or VSTO 2005 SE demonstrates the context menu that appears when the user selects and then right-clicks one or more items in a folder.
Date: 16-Jan-2008  13:15
Code level: intermediate
Code area: Outlook and .NET
Posted by: Sue Mosher
This message is displayed as VB.NET
 Public Class ThisAddIn

    Public Selection As Outlook.Selection

    Private Sub ThisAddIn_Startup(ByVal sender As Object, _
                                  ByVal e As System.EventArgs) Handles Me.Startup

    End Sub

    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, _
                                   ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub

    Private Sub Application_ItemContextMenuDisplay _
      (ByVal CommandBar As Microsoft.Office.Core.CommandBar, _
       ByVal Selection As Microsoft.Office.Interop.Outlook.Selection) _
      Handles Application.ItemContextMenuDisplay
        Dim i As Integer
        Dim AssignATask As Office.CommandBarPopup
        Dim ITStaff As String
        AssignATask = CommandBar.Controls.Add _
                      (Office.MsoControlType.msoControlPopup, , , , True)
        AssignATask.Caption = "Assign a Task"
        For i = 1 To 5
            If i = 1 Then
                ITStaff = "Name1"
                AssignTaskPopupMenus(ITStaff, AssignATask.CommandBar)
            ElseIf i = 2 Then
                ITStaff = "Name2"
                AssignTaskPopupMenus(ITStaff, AssignATask.CommandBar)
            ElseIf i = 3 Then
                ITStaff = "Name3"
                AssignTaskPopupMenus(ITStaff, AssignATask.CommandBar)
            ElseIf i = 4 Then
                ITStaff = "Name4"
                AssignTaskPopupMenus(ITStaff, AssignATask.CommandBar)
            ElseIf i = 5 Then
                ITStaff = "Name5"
                AssignTaskPopupMenus(ITStaff, AssignATask.CommandBar)
            End If
        Next i
        Me.Selection = Selection
    End Sub

    Private Sub Application_ContextMenuClose _
      (ByVal ContextMenu As Microsoft.Office.Interop.Outlook.OlContextMenu) _
      Handles Application.ContextMenuClose
        Selection = Nothing
    End Sub

    Private Sub AssignTaskPopupMenus(ByVal ITStaff As String, _
                                     ByVal myBar As Office.CommandBar)
        Dim Staff As Office.CommandBarPopup
        Dim JobPriorityLow As Office.CommandBarButton
        Dim JobPriorityMedium As Office.CommandBarButton
        Dim JobPriorityHigh As Office.CommandBarButton
        ' Main Popup
        Staff = myBar.Controls.Add _
                (Office.MsoControlType.msoControlPopup, , , , True)
        Staff.Caption = ITStaff
        ' Add 3 buttons to Popup
        JobPriorityLow = Staff.Controls.Add _
                         (Office.MsoControlType.msoControlButton, , , , True)
        JobPriorityLow.Caption = "Priority:Low"
        AddHandler JobPriorityLow.Click, AddressOf JobPriorityClickHandler

        JobPriorityMedium = Staff.Controls.Add _
                            (Office.MsoControlType.msoControlButton, , , , True)
        JobPriorityMedium.Caption = "Priority:Medium"
        AddHandler JobPriorityMedium.Click, AddressOf JobPriorityClickHandler

        JobPriorityHigh = Staff.Controls.Add _
                          (Office.MsoControlType.msoControlButton, , , , True)
        JobPriorityHigh.Caption = "Priority:High"
        AddHandler JobPriorityHigh.Click, AddressOf JobPriorityClickHandler

        Staff = Nothing
        JobPriorityHigh = Nothing
        JobPriorityLow = Nothing
        JobPriorityMedium = Nothing
    End Sub

    Sub JobPriorityClickHandler _
      (ByVal ctrl As Microsoft.Office.Core.CommandBarButton, _
       ByRef CancelDefault As Boolean)
        Dim c As Office.CommandBar
        Dim p As Office.CommandBarPopup
        c = TryCast(ctrl.Parent, Office.CommandBar)
        p = TryCast(c.Parent, Office.CommandBarPopup)
        If p IsNot Nothing Then
            MessageBox.Show("Selection count: " & Selection.Count.ToString, _
                            p.Caption & " - " & ctrl.Caption)
        End If
        p = Nothing
        c = Nothing
    End Sub

End Class
All 18comments
Page [ 1 2 Next >>  
  16-Jan-2008  13:19   
Notes on the code:

1) The sample was written and tested in Visual Studio 2008, but should also work in VSTO 2005 SE. It is for Outlook 2007 only; there are no equivalent events in earlier versions.

2) Two Application-level events are involved: ItemContextMenuDisplay fires before the context menu is displayed and should be used to add controls and wire event handlers. ContextMenuClose fires when the context menu disappears; use it to release an objects instantiated during the operation of the context menu's controls.

3) One class-level variable is involved -- Selection, representing the item(s) on which the user right-clicked -- not necessarily the same as ActiveExplorer.Selection.

4) Note that all the buttons are wired to a single event handler, which is aware both of the button that fired the Click event and its parent submenu.
  21-Jan-2008  11:34   
I am working on a VSTO 2005 SE context menu in C#. My code is almost exactly like this example, but I notice something stange. The CommandBar passed to ItemContextMenuDisplay never contains the submenu added in previous call, even through the events for the button on that submenu are still wired up. As a result, a submenu event will fire once for everytime the context menu has been displayed until outlook is shut down.

In C#, is is going to be necesarry to manual unwire all the submenu button clicks in the contextMenuClose event, or is there a way to persist the additions to the commandbar, or am I missing something else?

  21-Jan-2008  11:43   
Each time the ItemContextMenuDisplay event fires, your code needs to create the submenu and wire its buttons' events. I don't think that you need to unwire the events in ContextMenuClose, because the buttons will go out of scope, but of course, it wouldn't hurt to do the unwiring.
  18-Apr-2008  04:05   
To Sue. If you remember the example here is the code you sent to me for my system. I just wanted toask you if you had several sub menus from a button created on the Tools menu in Outlook 2007 if you knew how to get the Caption of one or any of the popup buttons. Thanks.
  18-Apr-2008  07:36   
Rudolph, isn't that covered here:

    Sub JobPriorityClickHandler _
      (ByVal ctrl As Microsoft.Office.Core.CommandBarButton, _
       ByRef CancelDefault As Boolean)
        Dim c As Office.CommandBar
        Dim p As Office.CommandBarPopup
        c = TryCast(ctrl.Parent, Office.CommandBar)
        p = TryCast(c.Parent, Office.CommandBarPopup)
        If p IsNot Nothing Then
            MessageBox.Show("Selection count: " & Selection.Count.ToString, _
                            p.Caption & " - " & ctrl.Caption)
  21-Apr-2008  05:42   
To Sue. I thought I could do this by adding the declaration

Dim nextpo As Office.CommandBarPopup
then have the line;

nextpo = TryCast(p.Parent, Office.CommandBarPopup)

so the line;


would display the 1st popup menu Caption when the last commandbar item is clicked, but the problem is I don't get anything displayed. Why is this?
  21-Apr-2008  08:41   
To Sue. Finally realised where I was going wrong. I need to have the following;

nextpo = TryCast(p.Parent.Parent, Office.CommandBarPopup)

Now when I use Msgbox(nextpo.Caption), I get the Caption for the popup above in the Tools menu. Thanks anyway.
  28-Apr-2008  12:38   
This is what I needed. Thanks Sue. One question ---
I want to conditionally show the right click menu depending upon the selection.
Public Selection As Outlook.Selection

Is it possible to find the store that theselection belongs to?

  28-Apr-2008  12:56   
SynDev, the Parent object of a Selection would be an Explorer, so you could use Selection.Parent.CurrentFolder.Store.
  29-Apr-2008  14:06   
I couldn't find the exact same property that you mentioned but I found this ---
This works for me. thank you for your direction.

Page [ 1 2 Next >>