2015-09-22 177 views
3

我正在使用VSTO在发送电子邮件时创建事件。目标是改变附件。 我已经有其他addins在ItemSend事件中运行,但问题是,我想我的插件先运行。因为我读,有在Outlook中没有执行顺序加载项发送的事件,但必须有某种秩序,即使只有名称或GUID ....C#VSTO Outlook ItemSend事件执行顺序

我尝试此解决方案(问题是,如果我有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  
    } 
} 

回答

1

this.Application.Inspectors.NewInspector + =新InspectorsEvents_NewInspectorEventHandler(Custom_Inspector);

获取NewInspector事件的Inspectors类激发你需要保持源对象活着,即防止它被垃圾收集器滑动。所以,我建议在全球范围内宣布一个Inspectors类的实例 - 在课堂上。

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

+0

尤金你能帮我在我的评论? – rockxl1

0

Eugene 100000谢谢!实际上Outlook Order Plugin Events按字母顺序排列。 但顺便说一下,如何设置NewInspector在顶级?我需要定义内部类ThisAddIn道具呼叫:

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

} 
+0

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