VSTO Outlook 插件 - SelectionChangeEventHandler 仅在启动时触发
VSTO Outlook Plugin - SelectionChangeEventHandler only fire on startup
我计划在每次收到所选 outlook 电子邮件时检索所选电子邮件的邮件项目,但事件句柄仅在启动时触发,有时会正确触发。我似乎无法找到导致问题的原因,大多数论坛都会进入死胡同,因此 post。
无论如何,这是我的启动方法的片段:
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) 是局部变量。一旦它超出范围,它就有资格被垃圾收集器释放。一旦发生这种情况,就不会引发任何事件。
将该变量的声明移动到 class 层。
我计划在每次收到所选 outlook 电子邮件时检索所选电子邮件的邮件项目,但事件句柄仅在启动时触发,有时会正确触发。我似乎无法找到导致问题的原因,大多数论坛都会进入死胡同,因此 post。
无论如何,这是我的启动方法的片段:
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) 是局部变量。一旦它超出范围,它就有资格被垃圾收集器释放。一旦发生这种情况,就不会引发任何事件。
将该变量的声明移动到 class 层。