Christian Treffler
Christian Treffler

Reputation: 63

VSTO Outlook AddIn: Cannot use Explorer Close event

some time ago I programmed an Outlook AddIn using NetOffice which worked very well. Now with the new Visual Studio Community 2017 I can program Office AddIns without the help of NetOffice. So I want to change my code, but I ran into a problem: I cannot subcsribe to the Explorer.Close event:

using Outlook = Microsoft.Office.Interop.Outlook;
namespace OLTest2
{
    public class ExplorerHandle
    {
        private Outlook.Explorer OutlookExplorer;
        public void InitExplorer(Outlook.Explorer expl)
        {
            OutlookExplorer = expl;
            // The next two lines compile:
            OutlookExplorer.BeforeItemPaste += BeforeItemPasteEvent;
            OutlookExplorer.SelectionChange += SelectionChangeEvent;

            // ***Next line does not compile***:
            OutlookExplorer.Close += CloseEvent; // "Cannot assign to 'Close' because it is a 'method group'"
            
            // This is the old NetOffice code which worked fine:
            /* 
            OutlookExplorer.BeforeItemPasteEvent += BeforeItemPasteEvent;
            OutlookExplorer.SelectionChangeEvent += SelectionChangeEvent;
            OutlookExplorer.CloseEvent += CloseEvent; 
            */ 
        }
    }
}

IntelliSense does not show me the existence of a Close event for an Outlook.Explorer object. But Microsoft tells me that such an event should exist:

Explorer events on MSDN

However, Visual Studio tells me that there's only a Close() method.

I'm missing something, but what?

Upvotes: 2

Views: 739

Answers (2)

Christian Treffler
Christian Treffler

Reputation: 63

Thanks to Dmitry, who pointed me in the right direction. The solution for the Close event is: ((Outlook.ExplorerEvents_10_Event)OutlookExplorer).Close += CloseEvent;

And in case somebody has a similar problem with OutlookInspector: A cast is necessary for e.g. the Activate event: ((Outlook.InspectorEvents_10_Event)OutlookInspector).Activate += ActivateEvent;

But I'm curious: Why do I have to cast to subscribe for Close, but not for BeforeItemPaste? According to the link I posted in my original question both events are "Inherited from ExplorerEvents_10_Event".

Upvotes: 2

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

You need to cast OutlookExplorer variable above to ExplorerEvents interface.

Upvotes: 2

Related Questions