ItemRemove event stop firing, ItemAdd & ItemChange still work

J

Jason

I'm in deep water now. The item deletion has been worked fine but suddenly
stop working. The ItemRemove event stops firing. But the add and change
still work fine. I don't know what cause that.

I do declare variables in the class level, following the advices in the
article "mstehle: The CDOs and CDONTS of Messaging Development"
http://blogs.msdn.com/mstehle/archi...rt-1-introduction-why-events-stop-firing.aspx.

Here is my code:

public partial class ThisAddIn
{
// appointment related
private Outlook.Explorer explorer;
private Outlook.Items items;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
try
{
/******* appointment related event handler registration
*********/

//explorer = this.Application.ActiveExplorer();
//explorer.SelectionChange += new
Outlook.ExplorerEvents_10_SelectionChangeEventHandler(ThisAddIn_SelectionChanged);

//openInspectors = this.Application.Inspectors;
//openInspectors.NewInspector += new
Outlook.InspectorsEvents_NewInspectorEventHandler(ThisAddIn_NewInspector);

Outlook._NameSpace ns =
this.Application.GetNamespace("MAPI");
Outlook.MAPIFolder calendarFolder =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
items = calendarFolder.Items;

items.ItemAdd += new
Outlook.ItemsEvents_ItemAddEventHandler(ThisAddIn_CalenmdarItemAdded);
items.ItemChange += new
Outlook.ItemsEvents_ItemChangeEventHandler(ThisAddIn_CalenmdarItemChanged);
items.ItemRemove += new
Outlook.ItemsEvents_ItemRemoveEventHandler(ThisAddIn_CalenmdarItemRemoved);
}
catch (Exception ex)
{
...
}
}

private void ThisAddIn_CalenmdarItemAdded(object item)
{
}

private void ThisAddIn_CalenmdarItemChanged(object item)
{
}

private void ThisAddIn_SelectionChanged()
{
}

private void ThisAddIn_CalenmdarItemRemoved()
{
}

private void ThisAddIn_NewInspector(Outlook.Inspector newInspector)
{
Marshal.ReleaseComObject(newInspector);
}
 
K

Ken Slovak - [MVP - Outlook]

private void ThisAddIn_NewInspector(Outlook.Inspector newInspector)
{
Marshal.ReleaseComObject(newInspector);
}

Once that fires the RCW for the new Inspector is completely released and any
further references to that object will fail.

Other than that the code looks OK to me offhand.

In some cases ItemRemove() won't fire if it's the last item in the Items
collection that's being deleted/moved/whatever. Could that be the case here?
ItemChange() should fire however in that case, is that what you're seeing?
 
J

Jason

Just thought the world collapsed and here came the savior. Thank you so much
Ken.

You are right that the remove event won't fire for the last item. I did some
cleanup this morning and no appointments was left, so the remove event stop
firing suddenly. I read that before but didn't pay much attention. Now it
happened to me.

If there is only 1 item, remove event won't fire. If there are more than 1
item, but select them all and delete them, remove event won't fire either.
There must be at least 1 item left.

Maybe it is a good idea to create a hidden dummy item/appointment somewhere,
and set the start date as far away such as year 2000?
 

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