c00000fd
c00000fd

Reputation: 22283

Disappearing Reply, Reply-All, and Forward event handlers in Outlook add-in?

I'm trying to catch events from my Outlook 2007 VSTO add-in when a Reply / Reply-All / Forward windows are activated. So I do this:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.ItemLoad += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemLoadEventHandler(onItemLoad);
}

Outlook.ItemEvents_10_Event gObj_itemEvt = null;

private void onItemLoad(object Item)
{
    gObj_itemEvt = (Outlook.ItemEvents_10_Event)Item;

    gObj_itemEvt.Reply += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReplyEventHandler(on_Reply);
    gObj_itemEvt.ReplyAll += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReplyAllEventHandler(on_ReplyAll);
    gObj_itemEvt.Forward += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ForwardEventHandler(on_Forward);
}

private void on_Reply(object Item, ref bool cancel)
{
    //Called when user loads Reply window
    LogMessage("Reply opened");
}

private void on_ReplyAll(object Item, ref bool cancel)
{
    //Called when user loads Reply-All window
    LogMessage("Reply-All opened");
}

private void on_Forward(object Item, ref bool cancel)
{
    //Called when user loads Forward window
    LogMessage("Forward opened");
}

This works for a few times, but then stops. In that case, I'm still getting onItemLoad events, but on_Reply, on_ReplyAll and on_Forward are not broadcast anymore.

What am I doing wrong here?

Upvotes: 1

Views: 219

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49435

You define a single object as a source for the events like Reply, ReplyAll or Forward. It means only a single item can be opened to handle these events. Let's imagine the following scenario when two items are opened and the ItemLoad event is fired. In the case of the first item, everything went well and the source item is set correctly. But the second event leads to re-writing the source item, so you will not hold a reference to the first item any longer and don't get events for it. If you want to handle events for all opened Outlook items you need to maintain a list of objects instead of a single object reference:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.ItemLoad += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemLoadEventHandler(onItemLoad);
}

List<Outlook.ItemEvents_10_Event> gObj_itemEvtList = new List<Outlook.ItemEvents_10_Event>();

private void onItemLoad(object Item)
{
    ItemEvents_10_Event gObj_itemEvt = (Outlook.ItemEvents_10_Event)Item;

    gObj_itemEvt.Reply += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReplyEventHandler(on_Reply);
    gObj_itemEvt.ReplyAll += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ReplyAllEventHandler(on_ReplyAll);
    gObj_itemEvt.Forward += new Microsoft.Office.Interop.Outlook.ItemEvents_10_ForwardEventHandler(on_Forward);

   gObj_itemEvtList.Add(gObj_itemEvt);
}

private void on_Reply(object Item, ref bool cancel)
{
    //Called when user loads Reply window
    LogMessage("Reply opened");
}

private void on_ReplyAll(object Item, ref bool cancel)
{
    //Called when user loads Reply-All window
    LogMessage("Reply-All opened");
}

private void on_Forward(object Item, ref bool cancel)
{
    //Called when user loads Forward window
    LogMessage("Forward opened");
}

Upvotes: 1

Related Questions