(VSTO SE) <folder>.Items.ItemAdd Event does not fire

D

David

I am trying to handle the ItemAdd event on a number of folders under my default mailbox in Outlook.

I find the Event fires for a while and then stops. Can anyone help explain why?



The following code is in from a VSTO SE AddIn.

1) I find a folder which I will handle the AddItem event for.

2) Call SetEventHandler which registers the event handler

3) Adds the a folder to a class level collection of folders

(Step 3 is to try an ensure the folder doesn't drop out of scope and get garbage collected)

4) Call SetEventHandler for each sub folder





private static SortedDictionary<String, Outlook.MAPIFolder> m_ItemAddHandled

= new SortedDictionary<string, Outlook.MAPIFolder>();



private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

Outlook.MAPIFolder myRootFolder = FindFolder(...);

if (colligoRootFolder != null)

{

SetEventHandler(myRootFolder);

}

}



private void SetEventHandler(Outlook.MAPIFolder parent)

{

System.Diagnostics.Debug.Print("SetEventHandler(" + parent.FullFolderPath + ")");



parent.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

m_ItemAddHandled.Add(parent.FullFolderPath, parent);



foreach (Outlook.MAPIFolder folder in parent.Folders)

{

SetEventHandler(folder);

}

}



void Items_ItemAdd(object Item)

{

MessageBox.Show("Items_ItemAdd");

}
 
D

Dmitry Streblechenko

parent.Items must be a class variable (see below in red) to make sure it stays referenced and alive to make sure it is not collect by the GC.
In case of multiple Items objects, use a list to store them.

private static SortedDictionary<String, Outlook.MAPIFolder> m_ItemAddHandled

= new SortedDictionary<string, Outlook.MAPIFolder>();



private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

Outlook.MAPIFolder myRootFolder = FindFolder(...);

if (colligoRootFolder != null)

{

SetEventHandler(myRootFolder);

}

}



public Outlook.Items m_Items;

private void SetEventHandler(Outlook.MAPIFolder parent)

{

System.Diagnostics.Debug.Print("SetEventHandler(" + parent.FullFolderPath + ")");



m_Items = parent.Items;

m_Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

m_ItemAddHandled.Add(parent.FullFolderPath, parent);



//foreach (Outlook.MAPIFolder folder in parent.Folders)

//{

// SetEventHandler(folder);

//}

}



void Items_ItemAdd(object Item)

{

MessageBox.Show("Items_ItemAdd");

}




Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
I am trying to handle the ItemAdd event on a number of folders under my default mailbox in Outlook.

I find the Event fires for a while and then stops. Can anyone help explain why?



The following code is in from a VSTO SE AddIn.

1) I find a folder which I will handle the AddItem event for.

2) Call SetEventHandler which registers the event handler

3) Adds the a folder to a class level collection of folders

(Step 3 is to try an ensure the folder doesn't drop out of scope and get garbage collected)

4) Call SetEventHandler for each sub folder





private static SortedDictionary<String, Outlook.MAPIFolder> m_ItemAddHandled

= new SortedDictionary<string, Outlook.MAPIFolder>();



private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

Outlook.MAPIFolder myRootFolder = FindFolder(...);

if (colligoRootFolder != null)

{

SetEventHandler(myRootFolder);

}

}



private void SetEventHandler(Outlook.MAPIFolder parent)

{

System.Diagnostics.Debug.Print("SetEventHandler(" + parent.FullFolderPath + ")");



parent.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

m_ItemAddHandled.Add(parent.FullFolderPath, parent);



foreach (Outlook.MAPIFolder folder in parent.Folders)

{

SetEventHandler(folder);

}

}



void Items_ItemAdd(object Item)

{

MessageBox.Show("Items_ItemAdd");

}
 
A

Anyone for Coffee?

Thanks for the response Dmitry.

Regarding your comment that parent.Items must be a class level variable. I
assume this is so that it is not garbage collected. I have tried to handle
this by putting the folder in the class-level "m_ItemAddHandled" collection.
Do you think this does not work?

It is difficult to use parent as a class level variable because I do not
know how many times the SetEventHandler will recurse.
 
D

Dmitry Streblechenko

No, saving MAPIFolder won't help since you need to keep a reference to the
object raising the evenst, i.e. Items.
If you do not know how many objects you will be referencing, store them in a
list which can have as many items as you want.

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

Anyone for Coffee?

Thanks Dmitry - that was it.

Is there a way of finding out what type of object has been added?
 
D

Dmitry Streblechenko

Sure, check the Class property using late binding, then cast the object
appropriately.

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

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