none
Add Context menu in AttachmentContextMenu in outlook Addin RRS feed

  • Question

  • HI All,

    I am trying to add a context menu item when right click on attachment in a mail.

    the code execute without error, but the added menu item not displaying.

    Please suggest, here is the code :

    Addin Startup : Application.AttachmentContextMenuDisplay += new ApplicationEvents_11_AttachmentContextMenuDisplayEventHandler(Application_AttachmentContextMenuDisplay);

    In Application_AttachmentContextMenuDisplay : Office.CommandBarButton customContextMenuTag = (Office.CommandBarButton)CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true);
    customContextMenuTag.Caption = "&My Context Menu";
    customContextMenuTag.FaceId = 351; //displays the image for the menu item
    customContextMenuTag.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;

    Thanks in advance.


    Dharmender Sharma

    Tuesday, September 17, 2013 11:38 AM

All replies

  • What version of Outlook?

    If you set a breakpoint in the context menu handler does the handler actually get executed?


    Ken Slovak MVP - Outlook

    Tuesday, September 17, 2013 6:12 PM
    Moderator
  • Hi Ken,

    I am using outlook 2010 and I tested after setting breakpoint. code get executed with no error and if I check the context menu control count it added in control but display is not visible.

    default context menu control count is 7 and after adding it increase it by one and it return me the caption of added control when I get it by control index on immediate window.

    Thanks 


    Dharmender Sharma

    Wednesday, September 18, 2013 4:57 AM
  • When you debug and get the control in the Immediate window what are the values for the Visible and Enabled properties of the control?

    What if you explicitly set Visible and Enabled to true, does the added menu item show up then?


    Ken Slovak MVP - Outlook

    Wednesday, September 18, 2013 2:52 PM
    Moderator
  • Hi Ken,

    I tried with visible and enable properties, but no success.

    Thanks

    Dharmender


    Dharmender Sharma

    Monday, September 23, 2013 7:57 AM
  • With Outlook 2010 you really should be using the newer context menu handling set up in the ribbon XML instead of the deprecated context menu events.

    Take a look at Randy's addin example for Outlook 2010 at http://msdn.microsoft.com/en-us/library/ee692172(v=office.14).aspx, it shows working with 2010 context menus (among other things).


    Ken Slovak MVP - Outlook

    Monday, September 23, 2013 2:13 PM
    Moderator
  • Hi Ken,

    Can't we add it through code ?? Please suggest.

    Thanks


    Dharmender Sharma

    Tuesday, September 24, 2013 5:30 AM
  • I've only tried 2010 context menus using ribbon XML. I haven't tried to see if Outlook 2007 style context menus work when using the code.

    I can try it later today in a test addin and see if it works. I'll post back here with my results.


    Ken Slovak MVP - Outlook

    Tuesday, September 24, 2013 3:27 PM
    Moderator
  • I tested with both Outlook 2007 and 2010 with the following code in a VSTO test COM addin:

       

    public partial class ThisAddIn


        {

            Outlook.

    Application olApp = null;

            Office.

    CommandBarButton customContextMenuTag = null;

           

    private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {

                olApp =

    this.Application;

                olApp.AttachmentContextMenuDisplay +=

    new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_AttachmentContextMenuDisplayEventHandler(olApp_AttachmentContextMenuDisplay);

            }

           

    void olApp_AttachmentContextMenuDisplay(Microsoft.Office.Core.CommandBar CommandBar, Microsoft.Office.Interop.Outlook.AttachmentSelection Attachments)

            {

                customContextMenuTag = (Office.

    CommandBarButton)CommandBar.Controls.Add(Office.MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true);

                customContextMenuTag.Caption =

    "&My Context Menu";

                customContextMenuTag.FaceId = 351;

    //displays the image for the menu item


                customContextMenuTag.Style = Microsoft.Office.Core.

    MsoButtonStyle.msoButtonIconAndCaption;

                customContextMenuTag.Visible =

    true;

                customContextMenuTag.Enabled =

    true;

                customContextMenuTag.Click +=

    new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(customContextMenuTag_Click);

            }

           

    void customContextMenuTag_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)

            {

                System.Windows.Forms.

    MessageBox.Show("Button clicked");

            }

           

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

            {

            }

    }

    The code worked as expected with Outlook 2007 and didn't work with Outlook 2010.

    As a result, I'd suggest using the recommended ribbon XML for handling the context menus in Outlook 2010.


    Ken Slovak MVP - Outlook

    Tuesday, September 24, 2013 5:03 PM
    Moderator
  • Thanks Ken,

    I tried using XML Ribbon, but in this case my custom ribbon buttons which I have added through code on addin startup they are not appearing.

    using xml it is showing context menu on attachment and working fine. so If I go with XML context menu I have to do rework for all options I given in ribbon through code. this project is almost done and tested and I don't want rework and retest again.

    Please guide.

    Thanks


    Dharmender Sharma

    Saturday, September 28, 2013 3:45 AM
  • Those are the only options I know of. If you don't want to rework your ribbon UI then I think you'll have to abandon the context menu handling.

    Ken Slovak MVP - Outlook

    Monday, September 30, 2013 2:10 PM
    Moderator