none
get objMailItem from saved mail ( *.msg ) RRS feed

  • Question

  • I use this code to get the current mailItem:

    var application = Globals.ThisAddIn.Application;
                    Outlook.Selection selection = application.ActiveExplorer().Selection;
                     mailItem = selection[1] as Outlook.MailItem;

    But when i use that code to get the mailItem which is opened by Outlook from file *.msg. That code can not get correct MailItem.

    So have another way to get this?

    Thanks.

    Tuesday, July 31, 2012 10:19 AM

Answers

  • Does this work?
     
        mailItem = (Outlook.MailItem)application.Inspectors[1].CurrentItem;

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:984a86fd-4193-4788-a6af-c54c661a5822...

    mail opened from disk does not appear in explorer window, to get it you should use application.ActiveInspector().CurrentItem

    Thanks. It's work but still have the problem:

    My current code now is:

    try
                    {
                        var application = Globals.ThisAddIn.Application;
                        mailItem = (Outlook.MailItem)application.ActiveInspector().CurrentItem;
                    }
                    catch (Exception)
                    {
                        try
                        {
                            var application = Globals.ThisAddIn.Application;
                            Outlook.Selection selection = application.ActiveExplorer().Selection;
                            mailItem = selection[1] as Outlook.MailItem;
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(Globals.ThisAddIn.Application.ToString());
                        }                 
                    }     

    And When i open the outlook, then open the mail saved from disk. It get the wrong mailItem ( it get the mail which is selected in outlook not the mail is opened from disk). 

    That means: application.ActiveInspector() is null with that case

    mailItem = (Outlook.MailItem)application.ActiveInspector().CurrentItem;


    Ken Slovak MVP - Outlook
    • Marked as answer by se7enbit Tuesday, July 31, 2012 5:49 PM
    Tuesday, July 31, 2012 2:14 PM

All replies

  • mail opened from disk does not appear in explorer window, to get it you should use application.ActiveInspector().CurrentItem

    Tuesday, July 31, 2012 11:29 AM
  • mail opened from disk does not appear in explorer window, to get it you should use application.ActiveInspector().CurrentItem

    Thanks. It's work but still have the problem:

    My current code now is:

    try
                    {
                        var application = Globals.ThisAddIn.Application;
                        mailItem = (Outlook.MailItem)application.ActiveInspector().CurrentItem;
                    }
                    catch (Exception)
                    {
                        try
                        {
                            var application = Globals.ThisAddIn.Application;
                            Outlook.Selection selection = application.ActiveExplorer().Selection;
                            mailItem = selection[1] as Outlook.MailItem;
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(Globals.ThisAddIn.Application.ToString());
                        }                 
                    }     

    And When i open the outlook, then open the mail saved from disk. It get the wrong mailItem ( it get the mail which is selected in outlook not the mail is opened from disk). 

    That means: application.ActiveInspector() is null with that case

    mailItem = (Outlook.MailItem)application.ActiveInspector().CurrentItem;

    • Edited by se7enbit Tuesday, July 31, 2012 1:36 PM
    Tuesday, July 31, 2012 1:32 PM
  • Does this work?
     
        mailItem = (Outlook.MailItem)application.Inspectors[1].CurrentItem;

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:984a86fd-4193-4788-a6af-c54c661a5822...

    mail opened from disk does not appear in explorer window, to get it you should use application.ActiveInspector().CurrentItem

    Thanks. It's work but still have the problem:

    My current code now is:

    try
                    {
                        var application = Globals.ThisAddIn.Application;
                        mailItem = (Outlook.MailItem)application.ActiveInspector().CurrentItem;
                    }
                    catch (Exception)
                    {
                        try
                        {
                            var application = Globals.ThisAddIn.Application;
                            Outlook.Selection selection = application.ActiveExplorer().Selection;
                            mailItem = selection[1] as Outlook.MailItem;
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(Globals.ThisAddIn.Application.ToString());
                        }                 
                    }     

    And When i open the outlook, then open the mail saved from disk. It get the wrong mailItem ( it get the mail which is selected in outlook not the mail is opened from disk). 

    That means: application.ActiveInspector() is null with that case

    mailItem = (Outlook.MailItem)application.ActiveInspector().CurrentItem;


    Ken Slovak MVP - Outlook
    • Marked as answer by se7enbit Tuesday, July 31, 2012 5:49 PM
    Tuesday, July 31, 2012 2:14 PM
  • when do you invoke your code? in add-in startup event? show us somewhat more of your code, not just single try block
    Tuesday, July 31, 2012 2:47 PM
  • Does this work?
     
        mailItem = (Outlook.MailItem)application.Inspectors[1].CurrentItem;

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    Ken Slovak MVP - Outlook

    Wa thanks a lot Ken Slovak :D.


    Tuesday, July 31, 2012 5:47 PM
  • when do you invoke your code? in add-in startup event? show us somewhat more of your code, not just single try block
    I load mail Item in the event Ribbon_Load. Thanks the problem was resolved.
    Tuesday, July 31, 2012 5:49 PM