Items_ItemAdd用于文件夹的监听器及其在Outlook中的子文件夹

时间:2013-07-29 13:12:06

标签: c# drag-and-drop outlook-addin comaddin

我正在为Outlook开发一些COM添加扩展,我希望在将项目放入特定文件夹或它的子文件夹时触发事件。我正在使用Items_ItemAdd方法来执行drop事件监听器。 如果将项目放入“父”文件夹,它可以正常工作,但当项目被放入子文件夹时没有任何反应。

以下是我正在使用的代码:

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是Outlook.Folder的列表,包含我想要监听的事件及其所有子文件夹的文件夹。

我正在Items_ItemAdd方法中听取此事件。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您需要在每个文件夹的Items集合上安装事件接收器。

要确保在您的应用运行时所有Items对象都处于活动状态,请将项目存储在列表中(例如List<Outlook.Items>

答案 1 :(得分: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);
    }