我正在使用 VSTO 在发送电子邮件时创建事件。目标是更改附件。
我已经有其他插件在 ItemSend
事件中运行,但问题是,我希望我的插件先运行。正如我所读到的,Outlook 插件发送事件没有执行顺序,但即使仅按名称或 guid 也必须有一些顺序...
我试过这个解决方案(问题是,如果我打开了 2 个邮件窗口,第一个窗口不会运行事件...:( 有一些覆盖事件问题)
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.Inspectors.NewInspector += new InspectorsEvents_NewInspectorEventHandler(Custom_Inspector);
//This run in the end off all ItemSend Events.... :(
//this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(MyFunction2);
}
private void Custom_Inspector(Inspector Inspector)
{
if (Inspector != null && Inspector.CurrentItem != null && Inspector.CurrentItem is Outlook.MailItem)
{
Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
if (mailItem.EntryID == null)
{
((ItemEvents_10_Event)mailItem).Send += new ItemEvents_10_SendEventHandler(MyFunction);
}
}
}
void MyFunction(ref bool Cancel)
{
MailItem mailItemContext = ((Inspector)this.Application.ActiveWindow()).CurrentItem as MailItem;
if (mailItemContext != null)
{
//my custom code here
}
}
最佳答案
this.Application.Inspectors.NewInspector += new InspectorsEvents_NewInspectorEventHandler(Custom_Inspector);
要触发 Inspectors 类的 NewInspector
事件,您需要使源对象保持事件状态,即防止它被垃圾收集器清除。因此,我建议在类级别的全局范围内声明 Inspectors 类的一个实例。
Outlook 对象模型不提供任何更改事件顺序的功能。根据我的经验,加载项是根据 ProgID 值(按字母顺序排序)加载的,事件以相反的顺序触发,即后进先出队列。
关于C# VSTO Outlook ItemSend事件执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32712399/