1
votes

I've a problem with my VSTO application for Outlook. I want to process the email body from a selected e-mail. For selected e-mails out of the "default" list this code works fine:

Object selItem = Globals.ThisAddIn.Application.ActiveExplorer().Selection[1];
        Outlook.MailItem mailItem = (Outlook.MailItem)selItem;
        return mailItem.Body;

But if a user opens an email from the list with a double click, the email is displayed in a new window. If the addin is executed in this window (over the ribbon), the email from the list is still used (which is now in the background).

Is there a way to find out if the plugin was started in a separate window and then get the email body from it?

Regards, Florian

2

2 Answers

2
votes

Coincidentally, I just dealt with something similar to this. My situation isn't identical, but since I could easily piece together what it seems like you're looking for see below. I haven't tested this, and obviously you'll have to handle passing the correct reference to your Outlook Application, but since i had this immediately available I figured it would pass it along with the hope that you'll find it helpful.

        private static void ribbonButton_Click(object sender, RibbonControlEventArgs e)
    {
        Outlook.Application application = new Outlook.Application();
        Outlook.Inspector inspector = application.ActiveInspector();

        if (application.ActiveExplorer().Selection[1] is Outlook.MailItem explorerMailItem)
        {
            // Write code to handle message if sourced from explorer (i.e., Reading Pane)
        }
        else if (inspector.CurrentItem is Outlook.MailItem inspectorMailItem)
        {
            // Write code to hanlde message if sourced from inspector 
            // (i.e., openened (double-clicked) message
        }
    }
1
votes

When you double click on email item you open an inspector window and you can access it by using Application.ActiveInspector() method. The Inspector object has CurrentItem property which represents the opened item. Also, you should avoid using multiple dots in expressions and properly release COM objects.