使用 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 = "";
}
}
}
}
}
我怀疑事件处理程序,我的想法是代码继续运行并且事件处理程序总是被添加(+=)从不删除(-=)我只是假设这是因为我对委托/事件处理程序的经验很少。谢谢