gpt4 book ai didi

c# - VSTO Outlook 插件 - SelectionChangeEventHandler 仅在启动时触发

转载 作者:行者123 更新时间:2023-11-30 22:56:38 28 4
gpt4 key购买 nike

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

无论如何,这是我的启动方法的一个片段:

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方法的一个片段:

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);
}
}

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

最佳答案

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

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

关于c# - VSTO Outlook 插件 - SelectionChangeEventHandler 仅在启动时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54286304/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com