C# VSTO Outlook ItemSend事件执行顺序

C# VSTO Outlook ItemSend Event execution order

我正在使用 VSTO 在发送电子邮件时创建事件。目标是改变附件。

我已经在 ItemSend 事件中有其他插件 运行,但问题是,我希望我的插件先到 运行。正如我所读,Outlook 加载项发送事件没有执行顺序,但即使仅按名称或 guid 也必须有一些顺序...

我尝试了这个解决方案(问题是,如果我打开了 2 封邮件 windows,第一个 window 不会 运行 事件... :(一些覆盖事件问题)

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

要触发 Inspector class 的 NewInspector 事件,您需要使源对象保持活动状态,即防止它被垃圾收集器清除。因此,我建议在全局范围内 - 在 class 级别声明 Inspectors class 的一个实例。

Outlook 对象模型不提供任何更改事件顺序的功能。根据我的经验,加载项是根据 ProgID 值(按字母顺序排序)加载的,事件以相反的顺序触发,即后进先出队列。

Eugene 100000 谢谢!实际上,Outlook 按字母倒序排列插件事件。 不过顺便说一句,如何在top class中设置NewInspector?我需要在 class ThisAddIn 中定义一个 prop 调用:

   public partial class ThisAddIn
{
        public Microsoft.Office.Interop.Outlook.Inspectors _inspector;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            _inspector = this.Application.Inspectors;
            _inspector.NewInspector += new InspectorsEvents_NewInspectorEventHandler(Custom_Inspector);
        }

}