Visual Studio Developer Center > Visual Studio Forums > Visual Studio Tools for Office > How can I add a button into the context menu when I right click the mail item in Outlook’s Explorer?
Ask a questionAsk a question
 

LockedHow can I add a button into the context menu when I right click the mail item in Outlook’s Explorer?

Locked

  • Sunday, February 08, 2009 12:28 PMJi.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How can I add a button into the context menu when I right click the mail item in Outlook’s Explorer?



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Answers

  • Sunday, February 08, 2009 12:29 PMJi.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    The approach to this problem depends on which version of Outlook we are working on.

    (1)    If we are working on Outlook 2007, we can easily achieve the objective by registering the Application.ItemContextMenuDisplay event. Then we have two parameters with a function to handle an event, one is the CommandBar object and another is the selected mail items. To add a button into the context menu, just call CommandBar.Controls.Add()

    See the discussion in this link:

     http://social.msdn.microsoft.com/forums/en-US/vsto/thread/74e3fbaf-5806-4684-b410-65a0720386e8/

    (2)    The Application.ItemContextMenuDisplay event was first introduced in Outlook 2007 object model. If we are working on Outlook 2003, we have to listen to CommandBars.OnUpdate event for this objective. But in the event handle, we do not have the parameter representing the corresponding command bar. We need to get that command bar by the following statement: Application.CommandBars[“Context Menu”].

    We can get the sample codes from this link:

    http://www.outlookcode.com/codedetail.aspx?id=314

     

    Please notice that: the “context menu” customization is not supported in Office 2003 by Microsoft. Using this code will be at your own risk. The workaround solution may break at some point due to a change in the Outlook 2003 product implementation. Officially it is not possible to customize the context menus for Outlook 2003.

    (Related forum thread http://social.msdn.microsoft.com/forums/en-US/vsto/thread/74e3fbaf-5806-4684-b410-65a0720386e8/ )




    For more FAQ about Visual Studio Tools for Office, please see Visual Studio Tools for Office FAQ



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

All Replies

  • Sunday, February 08, 2009 12:29 PMJi.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    The approach to this problem depends on which version of Outlook we are working on.

    (1)    If we are working on Outlook 2007, we can easily achieve the objective by registering the Application.ItemContextMenuDisplay event. Then we have two parameters with a function to handle an event, one is the CommandBar object and another is the selected mail items. To add a button into the context menu, just call CommandBar.Controls.Add()

    See the discussion in this link:

     http://social.msdn.microsoft.com/forums/en-US/vsto/thread/74e3fbaf-5806-4684-b410-65a0720386e8/

    (2)    The Application.ItemContextMenuDisplay event was first introduced in Outlook 2007 object model. If we are working on Outlook 2003, we have to listen to CommandBars.OnUpdate event for this objective. But in the event handle, we do not have the parameter representing the corresponding command bar. We need to get that command bar by the following statement: Application.CommandBars[“Context Menu”].

    We can get the sample codes from this link:

    http://www.outlookcode.com/codedetail.aspx?id=314

     

    Please notice that: the “context menu” customization is not supported in Office 2003 by Microsoft. Using this code will be at your own risk. The workaround solution may break at some point due to a change in the Outlook 2003 product implementation. Officially it is not possible to customize the context menus for Outlook 2003.

    (Related forum thread http://social.msdn.microsoft.com/forums/en-US/vsto/thread/74e3fbaf-5806-4684-b410-65a0720386e8/ )




    For more FAQ about Visual Studio Tools for Office, please see Visual Studio Tools for Office FAQ



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Wednesday, April 01, 2009 11:55 AMJi.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Add VB version codes,

    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            AddHandler Application.ItemContextMenuDisplay, AddressOf Application_ItemContextMenuDisplay
    End Sub

    Sub Application_ItemContextMenuDisplay(ByVal CommandBar As Microsoft.Office.Core.CommandBar, ByVal Selection As Microsoft.Office.Interop.Outlook.Selection)
            Dim cb As Office.CommandBarButton
            cb = CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, , , , True)
            cb.Visible = True
            cb.Caption = "NewButton"
    End Sub


    We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.