Archives

Currently Reading

Interesting TimesSoul MusicMen at ArmsLords and LadiesWitches AbroadReaper Man

More of Sytone's books »
Book recommendations, book reviews, quotes, book clubs, book trivia, book lists

Getting a MailItem from a Ribbon Event

You may find this code block handy:

        ///
        /// Gets the mail item selected in the explorer view if one is selected or instance if that is the view active.
        ///
        ///
The  instance containing the event data.
        /// A Outlook.MailItem for the mail being viewed.
        private Outlook.MailItem GetMailItem(RibbonControlEventArgs e)
        {
            // Check to see if a item is select in explorer or we are in inspector.
            if (e.Control.Context is Outlook.Inspector)
            {
                Outlook.Inspector inspector = (Outlook.Inspector)e.Control.Context;

                if (inspector.CurrentItem is Outlook.MailItem)
                {
                    return inspector.CurrentItem as Outlook.MailItem;
                }
            }

            if (e.Control.Context is Outlook.Explorer)
            {
                Outlook.Explorer explorer = (Outlook.Explorer)e.Control.Context;

                Outlook.Selection selectedItems = explorer.Selection;
                if (selectedItems.Count != 1)
                {
                    return null;
                }

                if (selectedItems[1] is Outlook.MailItem)
                {
                    return selectedItems[1] as Outlook.MailItem;
                }
            }

            return null;
        }
  • Adam

    You are a life saver!!!

  • Balu

    Thank you so much..its very useful

  • Bndiaye

    Hi, im a new in c#
    i want to know, when i call the function “”GetMailItem”, if i replace the RibbonControlEventArgs by my name of ribbon or button. 
    Thanks