Item Unload Event Doesn't Fire

G

Guest

I'm using VS 2K5, C#, VSTO SE and Outlook 2K7. I've created an Item event
handler class and hooked it up to the following events: AttachmentAdd, Save,
PropertyChange, Close and Unload. All the events fire as expected except
Unload. I've searched but I haven't found any info about a problem with this
event. I don't think I'm doing anything wrong but here's the code to hook the
Unload event just in case.

public class OLItemEventsHandler
{
protected Object m_oOLItem;
protected Outlook.ItemEvents_10_Event m_ItemEvents;
protected Outlook.ItemEvents_10_UnloadEventHandler
m_ItemEventsHandler_Unload;


public OLItemEventsHandler(object oOLItem)
{

m_oOLItem = oOLItem;

m_ItemEvents = (Outlook.ItemEvents_10_Event) oOLItem;

m_ItemEventsHandler_Unload = new
Microsoft.Office.Interop.Outlook.ItemEvents_10_UnloadEventHandler(itemEvent_Unload);

m_ItemEvents.Unload += m_ItemEventsHandler_Unload;
}


protected void itemEvent_Unload()
{
Trace.WriteLine("Item Unload Event Fired");
}

}
 
K

Ken Slovak - [MVP - Outlook]

Are you instantiating the item you want to handle Unload for in
Application_ItemLoad?

Unload won't necessarily fire when an item is closed, so if your handler is
going out of scope on Close you'd never get the event. It fires at some
point when Outlook unloads the item from it's in memory cache. That could be
seconds or minutes after closing the item. Items are "loaded" not only when
opening them but even by their being displayed in the reading pane.
 
G

Guest

Ken,

Thanks for the reply. I've solved the problem but don't understand why it
was happening.

After testing a bit more, I noticed that the NewInspector event didn't fire
after I sent a meeting request. So I stripped my code down to just the
NewInspector event handler. After the code change the NewInspector event only
fired once, the very first time an inspector was opened. I never received
another NewInspector event after that. I was handling the event in the
ThisAddIn class so I decided to create a new class to process the event. Once
I did this everything worked as expected.

I'm new to VSTO (all my previous Outlook addin work was done with C++ & ATL)
and I haven't read all the documentation so perhaps there's something I
missed. Any thoughts about why this was a problem? Are there any known
limitations on what can be done in the ‘ThisAddIn’ class?

Tim
 
K

Ken Slovak - [MVP - Outlook]

Was the inspectors object used for the NewInspector event declared at a
level that it won't go out of scope?

I usually declare a class level object for Inspectors in ThisAddIn and add
the event handler for NewInspector there. I use separate Inspector class
wrappers that are stored in a List or SortedList to keep the Inspector
classes alive. Inside each wrapper class I handle whatever events I'm
interested in for the Inspector and its CurrentItem (like a mail or
appointment item).

I do handle NewInspector and NewExplorer in ThisAddIn, but again those
objects are declared at the class (module) level.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top