2013-03-05 30 views
0

上的Outlook插件在工作,到目前爲止按預期工作的一切: - 外接負載 - 創建一個組合框和更改事件處理程序 - 創建時的事件處理程序用戶點擊新電子郵件在Outlook如何重置事件外接

取決於用戶在組合框中選擇的內容,將加載HTML模板或白色模板(僅返回原始狀態)。

經過幾次測試在兩個模板之間來回切換之後,變化事件不再被捕獲。你可以在代碼中看到下面我使用它來辦理變更事件:

cmboBxKeyWord.Change += new CommandBarComboBoxEvents_ChangeEventHandler(cmboBxKeyWord_Change); 

這裏是代碼的其餘部分:

public partial class ThisAddIn 
{ 
    private string templateName = ""; 
    private string subject=""; 
    private int messageType; 
    private string customerId; 
    private CommandBar menuBar; 
    private Outlook.Inspectors inspectors; 

    StringBuilder sb = new StringBuilder(); 

    private void ThisAddIn_Startup(object sender, System.EventArgs e) 
    { 
     CreateControl(); 

     inspectors = this.Application.Inspectors; 
     inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector); 
    } 

    private void CreateControl(){ 
      menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar; 
      CommandBarComboBox cmboBxKeyWord = (Office.CommandBarComboBox)menuBar.Controls.Add(Office.MsoControlType.msoControlComboBox, missing, missing, 1, true); 
      cmboBxKeyWord.AddItem("Template Email"); 
      cmboBxKeyWord.AddItem("Regular Email"); 
      cmboBxKeyWord.Change += new _CommandBarComboBoxEvents_ChangeEventHandler(cmboBxKeyWord_Change); 
    } 

    private void cmboBxKeyWord_Change(CommandBarComboBox Ctrl) 
    { 

     this.messageType = Ctrl.ListIndex - 1; 
     if (this.messageType == 1) 
     { 
      this.templateName = @"c:\templates\Blank Email.txt"; 
     } 
     else 
     { 
      this.templateName = @"c:\templates\Template Email.txt"; 
      this.customerId = Microsoft.VisualBasic.Interaction.InputBox("Enter Customer ID of the customer to which you want to send this email", "Customer ID"); 

      if (this.customerId == null || this.customerId.Length == 0) 
       this.messageType = 1; 
     } 
    } 

    private void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector) 
    { 
     Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem; 

     if (mailItem != null) 
     { 
      if (mailItem.EntryID == null) 
      { 
       if (this.messageType == 0) //HTML template email not the blank email... 
       { 

        mailItem.HTMLBody = getBody(); // the get body is a func that returns string, after fetching data from DB and processing it...nothing magic 
        mailItem.Subject = getSubject(); 
       } 
       else 
       { 
        mailItem.HTMLBody = ""; 
        mailItem.Subject = ""; 
       } 
      } 
     } 
    } 
} 

我懷疑我的事件處理程序,我的想法是代碼保持運行,事件處理程序總是被添加(+ =)永遠不會被刪除( - =)我只是假設這是因爲我的委託/事件處理程序的一點經驗。由於

回答

0
CommandBarComboBox cmboBxKeyWord = (Office.CommandBarComboBox)menuBar.Controls.Add(Office.MsoControlType.msoControlComboBox, missing, missing, 1, true); 
有待在類之外聲明

組合框,它超出範圍時,該過程就完成了,後來得到由GC收集...

相關問題