can not remove event handler in Outlook

Add-in Express™ Support Service
That's what is more important than anything else

can not remove event handler in Outlook
 
Guest


Guest


1.
is there a way to get the MapiFolder which is set as incoming mail folder by the user? I think this folder can be some user created folder other than inbox folder.

2.

I add eventhandlers to the folders with following code.


Outlook.MAPIFolder leafFolder = null;
int countFolders = //number of folders to add eventhandler//
for(int i=0; i<countFolders; i++)
{

// GetFolder Returns the folder using fullpath argument. different fullpath is given in each iteration of the loop.
leafFolder = GetFolder(fullPath);

try
{
// Add eventhandler
leafFolder.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(FolderWatch);
// put the folder object in an arraylist "olHandledFolders"
olHandledFolders.Add(leafFolder);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

After adding eventhandler, this code also adds the folder to an arraylist(olHandledFolders) containing folders which have eventhandler added to them.

That works fine.
But just after executing that block, following code raises exception:
"Object reference not set to an instance of an object."

int countFolders = olHandledFolders.Count;
for(int i=0; i<countFolders; i++)
{
try
{
// retrieve folder in arraylist olHandledFolders
Outlook.MAPIFolder folder = (Outlook.MAPIFolder)olHandledFolders[i];

// Remove the eventhandler added to it previously.
// This statement raises mentioned exception.
folder.Items.ItemAdd -= new Outlook.ItemsEvents_ItemAddEventHandler(FolderWatch);

olHandledFolders.RemoveAt(i);
Marshal.ReleaseComObject(folder);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}


Thanks in advance
serhat.
Posted 18 Oct, 2005 05:20:02 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Serhat.

1. You can access all Outlook folders via the Outlook._NameSpace interface (see the Folders property). To get an instance of namespace use the following code:
Outlook._NameSpace n = OutlookApp.GetNamespace("MAPI");

2. I think the problem is in the GC (garbage collector). GC clears your interfaces of Outlook folders with time. Try to add the following code before the 'olHandledFolders.Add(leafFolder);' line:

IntPtr unk = Marshal.GetIUnknownForObject(leafFolder)
leafFolder = Marshal.GetObjectForIUnknown(unk);

This code will increment the reference count on the specified Outlook folder interface.
Posted 18 Oct, 2005 06:58:10 Top
Guest


Guest


Sergei,

(as usual) it worked. Thanks a lot.

BTW, in the first question, I am asking if it is possible to get the folder which is set as the incoming mail folder by the user, not any folder, or not neccesserily inbox folder.

Or, is there a property (boolean may be) in MapiFolder class which indicates wheather the given folder is an incoming mail folder or not?

Thanks in advance.
Posted 18 Oct, 2005 08:45:08 Top
Serhat


Guest


Sergei,

When I try to remove folders from the arraylist they are contained, element at correct index can not be removed. Strange, but,

olHandledFolders.RemoveAt(i);

removed the i+1 th element.

Does it have something with the code you have posted?
I helped me to keep the object, but now I can not get rid of it.

Serhat
Posted 18 Oct, 2005 11:01:35 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Serhat, do you clear interfaces of folders via Marshal.ReleaseComObject method before you remove them from the collection?

As for incoming mail folder, I think it is possible to determine via Extended MAPI.
Posted 18 Oct, 2005 11:53:14 Top
Guest


Guest


Sergei,

My mistake, it removes perfectly. Sorry.

Serhat.
Posted 18 Oct, 2005 18:45:31 Top