Mapi Folder Items ItemChange event is not firing

A

AtulSureka

Hi,

I have used the following code to Handle the EditItem Event of Task Items

using System;
using System.Data;
using System.Collections;
using Outlook = Microsoft.Office.Interop.Outlook;

public class MyClass
{
private static Outlook.NameSpace nSpace;
private static Outlook.MAPIFolder mapiFolder;

public static void Main(string[] args)
{
Outlook.Application app = new
Microsoft.Office.Interop.Outlook.Application();
nSpace = app.GetNamespace("MAPI");

mapiFolder =
nSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
mapiFolder.Items.ItemChange += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(TaskItemChanged);

Console.ReadLine();
}

private static void TaskItemChanged(object Item)
{
Console.WriteLine("Item Has been chnaged");
}
}

However many times ItemChange event does not fire.
What can be the possible reasons?
Is there any work around for it?

- Atul Sureka
 
D

Dmitry Streblechenko

You need to keep MAPIFolder.Items collection in a global (class) variable to
make sure the GC does not release it.

public class MyClass
{
private static Outlook.NameSpace nSpace;
private static Outlook.Items Items
....

Items = mapiFolder.Items;
Items.ItemChange += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(TaskItemChanged);

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
J

Josh Einstein

Due to the way .NET and COM interop works, you have to cache the Items
object in a field as well. Just having the MAPIFolder isn't enough because
each call to Items will return a different RCW (runtime callable wrapper)
which means your first one goes out of scope and is subsequently garbage
collected.

private Outlook.Items _items;

_items = _mapiFolder.Items;
_items.ItemChange += delegate { MessageBox.Show("item changed!"); };

--
Josh Einstein
Einstein Technologies
Microsoft Tablet PC MVP
Tablet Enhancements for Outlook 2.0 - Try it free for 14 days
www.tabletoutlook.com
 
J

Josh Einstein

Whoops, guess you beat me to it.

--
Josh Einstein
Einstein Technologies
Microsoft Tablet PC MVP
Tablet Enhancements for Outlook 2.0 - Try it free for 14 days
www.tabletoutlook.com


Dmitry Streblechenko said:
You need to keep MAPIFolder.Items collection in a global (class) variable
to make sure the GC does not release it.

public class MyClass
{
private static Outlook.NameSpace nSpace;
private static Outlook.Items Items
...

Items = mapiFolder.Items;
Items.ItemChange += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(TaskItemChanged);

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

AtulSureka said:
Hi,

I have used the following code to Handle the EditItem Event of Task Items

using System;
using System.Data;
using System.Collections;
using Outlook = Microsoft.Office.Interop.Outlook;

public class MyClass
{
private static Outlook.NameSpace nSpace;
private static Outlook.MAPIFolder mapiFolder;

public static void Main(string[] args)
{
Outlook.Application app = new
Microsoft.Office.Interop.Outlook.Application();
nSpace = app.GetNamespace("MAPI");

mapiFolder =
nSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
mapiFolder.Items.ItemChange += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(TaskItemChanged);

Console.ReadLine();
}

private static void TaskItemChanged(object Item)
{
Console.WriteLine("Item Has been chnaged");
}
}

However many times ItemChange event does not fire.
What can be the possible reasons?
Is there any work around for it?

- Atul Sureka
 

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