C# VSTO Outlook ItemSend事件执行顺序

3
我正在使用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     
    }
}
2个回答

2
为了触发Inspectors类的NewInspector事件,您需要保持源对象的活动状态,即防止它被垃圾回收器清除。因此,我建议在全局范围内 - 在类级别上声明Inspectors类的一个实例。
根据我的经验,Outlook对象模型没有提供任何更改事件顺序的内容。插件基于ProgID值加载(按字母顺序排序),事件以相反的顺序触发,即一个LIFO队列。

尤金,你能帮我看一下我的注释吗? - Rafael Rocha

0

非常感谢Eugene!实际上,Outlook Order Plugin事件按字母顺序相反。 但是,顺便问一下,如何将NewInspector设置为顶级类? 我需要在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);
        }

}

rockxl1,你最终解决了这个问题吗?如果是的话,能否添加解决方案。非常感谢。 - seshuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接