Outlook アドインでの作業、これまでのところすべてが期待どおりに機能します: - アドインの読み込み - コンボボックスと変更イベント ハンドラーを作成します - ユーザーが新しい電子メールをクリックしたときのイベント ハンドラーを作成します
ユーザーがコンボボックスで選択したものに応じて、アドインは HTML テンプレートまたは白いテンプレート (元の状態に戻るため) のいずれかを読み込みます。
2 つのテンプレート間でいくつかのテストを行ったり来たりした後、変更イベントはキャッチされなくなりました。以下のコードでわかるように、これを使用して変更イベントを処理します。
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 = "";
}
}
}
}
}
私はイベント ハンドラーを疑っています。私の考えでは、コードは実行され続け、イベント ハンドラーは常に追加されます (+=) 決して削除されません (-=) デリゲート/イベント ハンドラーの経験が少ないため、これを想定しているだけです。ありがとう