Andrei Smolin

How to handle Outlook item’s Reply event: tracking Inspector.Activate

In How to handle Outlook item’s Reply event: tracking Explorer.SelectionChange, I explained the MailItem.Reply event and parameters it provides. Also, I described a class handling this event in a version-neutral way. That class shows a warning when the number of recipients is more than one. Now I’m going to use that class – named ReplyChecker – in an add-in intercepting the Reply event for an item shown in an inspector window. This is a pretext, of course. What I want is to show how you intercept Outlook’s Inspector.Activate event.

Activating an inspector window generates the Activate event on the corresponding Inspector object. To intercept that event, you need to pre-connect to the events of that Inspector object. This is possible if you manage a collection of Inspector wrappers, the wrapper class – it connects to the events of an Inspector – deals with inspectors created and closed.

The Outlook Events component in Add-in Express simplifies your task. It performs the above job for you. You avoid doing the work already done by someone else and write this code:


private void adxOutlookEvents_InspectorActivate(object sender,
    object inspector, string folderName)
    Outlook.Inspector theInspector = inspector as Outlook.Inspector;
    if (theInspector != null)
        object item = theInspector.CurrentItem;
        if (item is Outlook.MailItem)
            if (replyAllChecker.IsConnected)
            replyAllChecker.ConnectTo(item, true);

The event handler starts with getting the active inspector. Note that Outlook.Inspector objects are COM objects and you must release COM objects after use. The code fragment above doesn’t release theInspector because the event parameters are provided by Add-in Express and therefore, Add-in Express is responsible for releasing every COM object passed to this method. The Inspector object is used here to call Inspector.CurrentItem; this creates another COM object representing the Outlook item opened in the inspector window. Then, the add-in makes sure that the current item is a mail item and reconnects the ReplyChecker class to the events of the mail item.

When dealing with COM add-ins, the best strategy is to release everything scrupulous. Let’s look how the above code releases the COM object created by Inspector.CurrentItem. First off, it is released in case the inspector contains an item of a type other than MailItem. In case, the item is of the MailItem type, it is released when ReplyChecker is disconnected from the item’s events. This is specified by the second parameter of the ConnectTo method; the parameter name is eventClassReleasesComObject. Actually, the ConnectTo and RemoveConnection methods are described in my previous post as follows:

“These code lines disconnect the previously connected MaiItem object and pass the selected mail item to the ADXOutlookItemEvents.ConnectTo() method; the second parameter of this method specifies whether to release that COM object when ADXOutlookItemEvents.RemoveConnection() is called.”

Good luck!

Available downloads:

This sample COM Add-in was developed using Add-in Express for Office and .net:

C# sample COM add-in
VB.NET sample COM add-in

Post a comment

Have any questions? Ask us right now!