none
get mail item opened (no selected) in outlook c# RRS feed

  • Question

  • I want to know how to get the item of opened mail from outlook using C#, i have already do for the seleced mail in inbox but i don't know how to proceded for the opened or writed mail.

    thanks!

    Tuesday, December 27, 2011 11:11 AM

Answers

  • Hello,

    You need to cast the value returned by OutlookApp.ActiveInspector().CurrentItem to an appropriate type e.g. MailItem. Pay attention to the need to release COM objects: in the code fragment above the COM object returned by OutlookApp.ActiveInspector() is left unreleased. What's wrong with this and how to bypass problems this approach can produce, see in my article When to release COM objects in Office add-ins developed in .NET.

     


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader
    Tuesday, December 27, 2011 1:04 PM
  • thank 

        public Outlook.MailItem GetMailItem(RibbonControlEventArgs e)
        {
            // Check to see if an item is selected 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;
        }
    


    Tuesday, December 27, 2011 3:10 PM

All replies

  • Hello,

    You need to cast the value returned by OutlookApp.ActiveInspector().CurrentItem to an appropriate type e.g. MailItem. Pay attention to the need to release COM objects: in the code fragment above the COM object returned by OutlookApp.ActiveInspector() is left unreleased. What's wrong with this and how to bypass problems this approach can produce, see in my article When to release COM objects in Office add-ins developed in .NET.

     


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader
    Tuesday, December 27, 2011 1:04 PM
  • thank 

        public Outlook.MailItem GetMailItem(RibbonControlEventArgs e)
        {
            // Check to see if an item is selected 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;
        }
    


    Tuesday, December 27, 2011 3:10 PM
  • Hi bndiaye,

     

    Thanks for posting in the MSDN Forum.

     

    I’m glad to hear you solved your issue. Thanks for sharing you solution here! It’s very beneficial for other community members who have similar problem to see how you solved your issue.

     

    @Andrei,

     

    Thanks for your great work.

     

    Have a good day,

     

    Tom

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 2, 2012 9:01 AM
    Moderator