A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

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



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old March 6th 07, 11:56 PM posted to microsoft.public.outlook.program_vba
David
external usenet poster
 
Posts: 6
Default (VSTO SE) folder.Items.ItemAdd Event does not fire

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 SortedDictionaryString, Outlook.MAPIFolder m_ItemAddHandled

= new SortedDictionarystring, 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_Item Add);

m_ItemAddHandled.Add(parent.FullFolderPath, parent);



foreach (Outlook.MAPIFolder folder in parent.Folders)

{

SetEventHandler(folder);

}

}



void Items_ItemAdd(object Item)

{

MessageBox.Show("Items_ItemAdd");

}

  #2  
Old March 7th 07, 12:29 AM posted to microsoft.public.outlook.program_vba
Dmitry Streblechenko
external usenet poster
 
Posts: 2,116
Default (VSTO SE) folder.Items.ItemAdd Event does not fire

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 SortedDictionaryString, Outlook.MAPIFolder m_ItemAddHandled

= new SortedDictionarystring, 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_Item Add);

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
"David" wrote in message ...
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 SortedDictionaryString, Outlook.MAPIFolder m_ItemAddHandled

= new SortedDictionarystring, 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_Item Add);

m_ItemAddHandled.Add(parent.FullFolderPath, parent);



foreach (Outlook.MAPIFolder folder in parent.Folders)

{

SetEventHandler(folder);

}

}



void Items_ItemAdd(object Item)

{

MessageBox.Show("Items_ItemAdd");

}

  #3  
Old March 7th 07, 01:39 AM posted to microsoft.public.outlook.program_vba
Anyone for Coffee?
external usenet poster
 
Posts: 18
Default (VSTO SE) folder.Items.ItemAdd Event does not fire

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.
  #4  
Old March 7th 07, 06:05 AM posted to microsoft.public.outlook.program_vba
Dmitry Streblechenko
external usenet poster
 
Posts: 2,116
Default (VSTO SE) folder.Items.ItemAdd Event does not fire

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

"Anyone for Coffee?" wrote in
message ...
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.



  #5  
Old March 7th 07, 05:59 PM posted to microsoft.public.outlook.program_vba
Anyone for Coffee?
external usenet poster
 
Posts: 18
Default (VSTO SE) folder.Items.ItemAdd Event does not fire

Thanks Dmitry - that was it.

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

  #6  
Old March 7th 07, 08:41 PM posted to microsoft.public.outlook.program_vba
Dmitry Streblechenko
external usenet poster
 
Posts: 2,116
Default (VSTO SE) folder.Items.ItemAdd Event does not fire

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

"Anyone for Coffee?" wrote in
message ...
Thanks Dmitry - that was it.

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



 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Word editor does not fire Shutdown event toni santa Outlook and VBA 1 February 2nd 07 07:28 PM
ItemAdd in Deleted Items event doesnt fire after a while [email protected] Add-ins for Outlook 11 January 8th 07 08:27 AM
ItemAdd in Deleted Items event doesnt fire after a while SHUperb Outlook - Calandaring 0 January 5th 07 07:41 AM
ItemAdd event with Outlook2007 DavidH&P Outlook - Using Forms 2 July 4th 06 08:29 AM
script event does not fire urs Outlook - General Queries 2 March 1st 06 04:55 PM


All times are GMT +1. The time now is 12:25 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.