VSTO ItemAdd事件未触发

问题描述 投票:0回答:1

我有以下VSTO加载项代码。然而,items_ItemAdd()方法并没有被暗杀。

public partial class ThisAddIn
    {
        Outlook.NameSpace outlookNameSpace;
        Outlook.MAPIFolder inbox;
        Outlook.Items items;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            outlookNameSpace = this.Application.GetNamespace("MAPI");
            Debug.WriteLine("Starting Add-In");
            inbox = outlookNameSpace.GetDefaultFolder(
                    Microsoft.Office.Interop.Outlook.
                    OlDefaultFolders.olFolderInbox);
            items = inbox.Items;
            items.ItemAdd +=
                new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
            Debug.WriteLine("Started Add-In");
        }

        void items_ItemAdd(object Item)
        {
            Debug.WriteLine("New email arrived.");
            Outlook.MailItem mail = (Outlook.MailItem)Item;
            if (Item != null)
            {
                if (mail.MessageClass == "IPM.Note")
                {
                    string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "example.html");
                    Debug.WriteLine("Saving to " + path);
                    mail.SaveAs(path, Outlook.OlSaveAsType.olHTML);
                }
            }
        }

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

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }

这与MSDN中给出的例子非常相似有没有人有想法?我正在使用VSTO Outlook 2007加载项在Visual Studio 2010中进行开发。在调试模式下尝试此操作但事件未触发。我在我的开发机器上安装了Outlook 2010。

c# outlook vsto
1个回答
0
投票

我无法让ItemsEvents_ItemAddEventHandler工作,尽管在课堂上宣布一些成员来解决垃圾收集问题,因为这似乎是互联网上随处可见的唯一答案......

在附加事件时添加稍微延迟的工作是什么,如下所示:

System.Threading.Timer timer = null;
timer = new System.Threading.Timer((obj) =>
{
    ItemAddEventHandler();
    timer.Dispose();
}, null, 3000, System.Threading.Timeout.Infinite);

我确实发现NewMailEx事件更可靠但它有它的缺点,例如无法选择文件夹来监听事件。希望这可以帮助某人,尽管它不是任何措施的干净解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.