none
Add Item_ItemAdd listener to multiple folders and it's subfolders in Outlook C# RRS feed

  • Question

  • Hi,

    I want to to listen for drop event when user drops item into some custom folder which contains multiple sub folders. If I add new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd) to one folder it works fine, but I can't figure out how to add it to folder and its sub folders.

    Here is my code for adding listener to sub folders:

    private List<Outlook.Folder> foldersPaths = new List<Outlook.Folder>();

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

    //here I fill folderParhs list with folder on which I want to listen for events and its subfolders if (foldersPaths.Count > 0) { foreach (Outlook.Folder folder in foldersPaths) { costumUserFolder = folder.Items; userFolderPath = folder.FolderPath; costumUserFolder.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd); } } }

    In this case it works just for last folder in list.

    I assume that I am doing something wrong in foreach loop.


    Monday, July 29, 2013 1:48 PM

Answers

  • The usual way to do something like that is to create a class that wraps the functionality you want to use, for example a Folder object class that has the event handlers you want to handle and that has a Folder object.

    Assign each folder and subfolder you want to monitor to a new instance of the wrapper class, and add that wrapper to a list/dictionary/collection/whatever to keep the class instance alive and not garbage collected.

    When the Add() event fires it fires for that folder only. The list or whatever must be declared at a scope where it will remain alive and in scope for the life of the code, for example at the class level of ThisAddin.


    Ken Slovak MVP - Outlook

    • Marked as answer by Jernej Grosar Tuesday, July 30, 2013 6:44 AM
    Monday, July 29, 2013 2:14 PM

All replies

  • The usual way to do something like that is to create a class that wraps the functionality you want to use, for example a Folder object class that has the event handlers you want to handle and that has a Folder object.

    Assign each folder and subfolder you want to monitor to a new instance of the wrapper class, and add that wrapper to a list/dictionary/collection/whatever to keep the class instance alive and not garbage collected.

    When the Add() event fires it fires for that folder only. The list or whatever must be declared at a scope where it will remain alive and in scope for the life of the code, for example at the class level of ThisAddin.


    Ken Slovak MVP - Outlook

    • Marked as answer by Jernej Grosar Tuesday, July 30, 2013 6:44 AM
    Monday, July 29, 2013 2:14 PM
  • Thank you very much. With your help I solved this in 5 minutes.

    Have a nice day!

    Jernej Grosar

    Tuesday, July 30, 2013 6:45 AM
  • hi

    could you share you solution? i am new to this and facing the same problem. I was unable to figure out how to "wrap" the functionality in a class. i am using VB.net but i suppose they are similar.

    thanks in advance!

    Monday, April 14, 2014 3:45 AM
  • Essentially you need to make sure the objects that raises the events are alive and not garbage collected.

    Create a list of objects (List<MAPIFolder>); when you process a folder, set up the event handler, then add the object to the list.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, April 14, 2014 4:22 AM
  • Could you please share you solution..I tried by creating a new class and still not succeeded. please let me know is there any wrong with this

    I did something like this 

      List<OutlookItems> listOutlookItems = new List<OutlookItems>();

     foreach (Outlook.MAPIFolder folder in rootFolder.Folders)
    {

    OutlookItems oi = new OutlookItems();
    oi.Folder = folder;
    listOutlookItems.Add(oi);

    }

    OutlookItems oii = new OutlookItems();
    oii.AddItem_Startup();

     and the created class was like this 

        public class OutlookItems
        {
            private MAPIFolder _folder;
            public MAPIFolder Folder { get; set; }
            public void AddItem_Startup()
            {
                Folder.Items.ItemAdd += new ItemsEvents_ItemAddEventHandler(ItemAdded);
            }
            private void ItemAdded(object Item)
            {
                Connect cn = new Connect();
                cn.test(Item);
            }
        }

    thank you



    • Edited by Hashsb Monday, January 29, 2018 11:53 AM
    Monday, January 29, 2018 11:45 AM