Andrei Smolin

How to handle Outlook item’s Reply event: tracking Explorer.SelectionChange

Glad you are here!

The Reply event of the MailItem object provides two parameters:

  • Response – this is a MailItem object representing the reply just created.
  • Cancel – obviously, you can cancel the process if a certain condition is met.

Add-in Express implements a version-independent approach to handling events of an Outlook item. The class implementing this is called ADXOutlookItemEvents. I recommend that you regard this class as a set of event handlers; you can implement several such classes to follow several business rules.

To use such a class, you add a descendant of ADXOutlookItemEvents to your add-in project, right from the Add New Item dialog.

The newly created class already contains a number of methods whose names follow this pattern “Process” + {Outlook item’s event name}; e.g. for the Reply event, there’s a method ProcessReply.

And here’s what the add-in was written for, an event handler of the MailItem.Reply event:

C#:

public override void ProcessReply(object response, AddinExpress.MSO.ADXCancelEventArgs e)
{
    if (this.ItemObj is Outlook.MailItem)
    {
        Outlook.MailItem mail = this.ItemObj as Outlook.MailItem;
        Outlook.Recipients recipients = mail.Recipients;
        if (recipients.Count > 1)
        {
            if (MessageBox.Show("Did you mean to click 'Reply All' ?",
                "ReplyAll sample project", MessageBoxButtons.YesNo)
                == DialogResult.Yes)
            {
                e.Cancel = true;
            }
        }
        Marshal.ReleaseComObject(recipients);
    }
}

That is, if the number of recipients of a certain email is 2 or more, the add-in lets you correct your mistake – in other words, the add-in warns you if you click Reply when you most likely intended to click Reply All.

The code above can be found in OutlookItemEventsClass1.cs. An instance of OutlookItemEventsClass1 is created and managed in AddinModule.cs, see the variable named itemEvents.

So far so good. Now I strive to talk about the way the add-in deals with tracing what the user selects in the explorer window.

Explorer.SelectionChange is an easy event. Add-in Express makes it even easier: the Outlook Events component hides complexities of dealing with several explorer windows. With this component, you create an event handler for the Explorer.SelectionChange event in this way:

C#:

private void adxOutlookEvents_ExplorerSelectionChange(object sender, object explorer)
{
    ConnectToSelectedItem(explorer);
}
private void ConnectToSelectedItem(object explorer)
{
    Outlook.Explorer theExplorer = explorer as Outlook.Explorer;
    if (theExplorer != null)
    {
        Outlook.Selection selection = null;
        try
        {
            selection = theExplorer.Selection;
        }
        catch { }
        if (selection != null)
        {
            if (selection.Count == 1)
            {
                object item = selection.Item(1);
                if (item is Outlook.MailItem)
                {
                    if (replyAllChecker.IsConnected)
                        replyAllChecker.RemoveConnection();
                    replyAllChecker.ConnectTo(item, true);
                }
                else
                    Marshal.ReleaseComObject(item);
            }
            Marshal.ReleaseComObject(selection);
        }
    }
}

The method ConnectToSelectedItem starts with getting an Outlook.Selection object; the way it does this is a must. It deals with this problem: getting Explorer.Selection can produce an exception. Have you ever got Explorer.Selection when the current folder is a top-level folder e.g. Personal Folders in Outlook 2000-2007? Or, with Outlook 2010, when the current folder is RSS Feeds? Be aware!

The remaining part of the method is almost a bore: the add-in makes sure that just one item is selected, that the item is an Outlook.MailItem and then connects to this item.

Just in case you’ve missed my previous blogs on releasing everything in Outlook development, see When to release COM objects in Office add-ins. So, the add-in gets, uses and releases: the Outlook.Selection object, an Outlook item of any type except for MailItem. As to MailItem objects, we connect to their events:

C#:

if (replyAllChecker.IsConnected)
    replyAllChecker.RemoveConnection();
replyAllChecker.ConnectTo(item, true);

These code lines disconnect the previously connected MaiItem object and pass the selected 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!