【问题标题】:VSTO Outlook Plugin - SelectionChangeEventHandler only fire on startupVSTO Outlook 插件 - SelectionChangeEventHandler 仅在启动时触发
【发布时间】:2019-01-22 14:23:46
【问题描述】:

我计划在每次收到所选 Outlook 电子邮件时检索所选电子邮件的邮件项,但事件句柄仅在启动时触发,有时会正确触发。我似乎找不到导致问题的原因,大多数论坛都会导致死胡同,因此这篇文章。

无论如何,这是我的启动方法的sn-p:

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

        this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);

        Outlook.Explorer currentExplorer = this.Application.ActiveExplorer();
        currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);

        outlookNameSpace = this.Application.GetNamespace("MAPI");
        inbox = outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
        items = inbox.Items;
        items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);

    }

这是我的 CurrentExplorer_Event 方法的 sn-p:

private void CurrentExplorer_Event()
    {
        newSelectedEmail = new Email();
        Outlook.MAPIFolder selectedFolder = this.Application.ActiveExplorer().CurrentFolder;
        try
        {
            if (this.Application.ActiveExplorer().Selection.Count > 0)
            {
                Object selObject = this.Application.ActiveExplorer().Selection[1];
                if (selObject is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
                    GetEmailInfoFromOutlookEmail(mailItem);
                }

            }
        }
        catch (Exception ex)
        {
            Operations.SaveLogToFile(LogType.Error, "Main - CurrentExplorer_Event", ex.Message, ex.StackTrace);
        }
    }

非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: c# vsto outlook-addin


    【解决方案1】:

    引发事件的变量 (currentExplorer) 是一个局部变量。一旦超出范围,它就有资格被垃圾收集器释放。一旦发生这种情况,就不会引发任何事件。

    将该变量的声明移至类级别。

    【讨论】:

    • 这救了我的命。非常感谢,德米特里!
    • 如果它回答了您的问题,请将其标记为这样。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    相关资源
    最近更新 更多