none
Outlook AddIn context menu does not recognize MailItem RRS feed

  • Question

  •   

    Hi, 

      I just start program for Outlook in Visual Studio. I plan to start from creating right click menu in Outlook Home email panel.  I got the example code from Context Menu for a Mail Item of Extending the User Interface in Outlook 2010. When I test it, I select a email in Outlook, right click my context menu item, it does not think it's a MailItem. What's wrong in my code? How I can debug it? Do you know any good book/blog/example/tutorial? 

      Here is the code:     

      
        <contextMenus>
        <contextMenu idMso="ContextMenuMailItem">
          <button id="MyContextMenuMailItem"
              label="OAIContextMenuMailItem"
              onAction="OnMyContextMenu_Click"/>
        </contextMenu>
      </contextMenus>
    
            public void OnMyContextMenu_Click(Office.IRibbonControl control)
            {
                System.Windows.Forms.MessageBox.Show("Your Context Menu Works!");
    
                if (control.Context is Outlook.Explorer)
                {
                    Outlook.Explorer explorer = control.Context as Outlook.Explorer;
    
                    Outlook.Selection selection = explorer.Selection;
                    if (selection.Count == 1)
                    {
                        if (selection[1] is Outlook.MailItem)
                        {
                            Outlook.MailItem oMail = selection[1] as Outlook.MailItem;
                            //return oMail.Sent;
    
                            System.Windows.Forms.MessageBox.Show("Sent");
                        }
                        else
                            System.Windows.Forms.MessageBox.Show("Not MailItem");
                    }
                    else
                        System.Windows.Forms.MessageBox.Show("Multiple selection");
                }
                else if (control.Context is Outlook.Inspector)
                {
                    Outlook.Inspector oInsp = control.Context as Outlook.Inspector;
                    if (oInsp.CurrentItem is Outlook.MailItem)
                    {
                        Outlook.MailItem oMail = oInsp.CurrentItem as Outlook.MailItem;
                        //return oMail.Sent;
    
                        System.Windows.Forms.MessageBox.Show("Sent at Inspector");
                    }
                    else
                        System.Windows.Forms.MessageBox.Show("Not MailItem at Inspector");
                }
                else
                    System.Windows.Forms.MessageBox.Show("not a Explorer/Inspector");
            }
    

    Thank you so much

    Wes


    Wes

    Monday, February 2, 2015 8:40 PM

Answers

  • Hello Wes,

    Outlook uses the MessageClass property for identifying what type of inspector to show for the item. The late-binding technology can be used to get the property value at runtime without casting the object to the MailItem type. See Type.InvokeMember for more information.

    selection[1].GetType().InvokeMember(...);

    You can also check out the item type using any low-level property viewer such as MFCMAPI or OutlookSpy.

    • Proposed as answer by Lanlan0901 Friday, February 13, 2015 8:25 PM
    • Marked as answer by L.HlModerator Tuesday, February 17, 2015 7:32 AM
    Tuesday, February 3, 2015 11:20 AM

All replies

  • Can you try to retrieve the Class property using reflection?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, February 2, 2015 11:44 PM
  • Hello Wes,

    Outlook uses the MessageClass property for identifying what type of inspector to show for the item. The late-binding technology can be used to get the property value at runtime without casting the object to the MailItem type. See Type.InvokeMember for more information.

    selection[1].GetType().InvokeMember(...);

    You can also check out the item type using any low-level property viewer such as MFCMAPI or OutlookSpy.

    • Proposed as answer by Lanlan0901 Friday, February 13, 2015 8:25 PM
    • Marked as answer by L.HlModerator Tuesday, February 17, 2015 7:32 AM
    Tuesday, February 3, 2015 11:20 AM