0

I am developing some COM add in extension for Outlook and I want to trigger event when item is dropped into specific folder or it's sub folder. I'm using Items_ItemAdd method for drop event listener. It works fine if item is dropped into "parent" folder, but nothing happens when item is dropped into sub folder.

Here is code I'm using:

private void ThisAddIn_Startup(object sender, System.EventArgs e
{
    foreach (Outlook.Folder folder in foldersPaths)
    {
       costumUserFolder = folder.Items;
       costumUserFolder.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
    }
}

foldersPaths is List of Outlook.Folder and contains folder I want to listen to for events and all it's sub folders.

I am listening for this events in Items_ItemAdd method.

Any ideas?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
misterGrosar
  • 294
  • 1
  • 3
  • 10

2 Answers2

2

You need to install the event sink on each folder's Items collection.

To make sure all Items objects are alive while your app runs, store Items in a list (e.g. List<Outlook.Items>)

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0
    //Please find the implemented tested working Solution:


    Outlook.Items oMailItems = null; //Globally declared object
    List<Outlook.Items> allInboxFolder = new List<Outlook.Items>(); //Globally declared
Outlook.MAPIFolder inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
    //Implemented Threading for each item recieved to Inbox Folder
    //Outlook.Items oMailItems = null; //Globally declared object
    oMailItems = inbox.Items;
    oMailItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(ThreadStarter);
    allInboxFolder.Add(oMailItems); //make all subfolders events live
    foreach (Outlook.Folder folder in inbox.Folders)
    {
        oMailItems = folder.Items;
        oMailItems.ItemAdd += new   Outlook.ItemsEvents_ItemAddEventHandler(ThreadStarter);
        allInboxFolder.Add(oMailItems);
    }

    private void ThreadStarter(Object Item)
    {
        //InboxFolderItemAdded invoked by thread
        System.Threading.Thread IncomingMailThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(this.InboxFolderItemAdded));
        IncomingMailThread.IsBackground = true;
        IncomingMailThread.Start(Item);
    }