单击自定义按钮后,Outlook事件不会被触发

时间:2013-01-03 09:35:33

标签: outlook vsto add-in outlook-addin outlook-2010

我正在开发一个Microsoft Outlook加载项,我在加载项选项卡名OPENISMS中添加了一个按钮。我可以看到按钮,但是点击该事件不会被触发。我不知道为什么它以这种方式表现。请在下面找到添加按钮和附加事件的代码。任何帮助将受到高度赞赏。

private void AddButtonToNewDropdown()
{
    Office.CommandBar commandBar = this.Application.ActiveExplorer().CommandBars["Standard"];
    Office.CommandBarControl ctl = commandBar.Controls["&New"];
    if (ctl is Office.CommandBarPopup) 
    {
        Office.CommandBarButton commandBarButton;
        Office.CommandBarPopup newpopup = (Office.CommandBarPopup)ctl;
        commandBarButton = (Office.CommandBarButton)newpopup.Controls.Add(1, missing, missing, missing, true);
        commandBarButton.Caption = "OpenISMS";
        commandBarButton.Tag = "OpenISMS";
        commandBarButton.FaceId = 6000;
        //commandBarButton.Enabled = false;
                      commandBarButton.OnAction = "OpenISMSThruMail.ThisAddIn.ContextMenuItemClicked";
        commandBarButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ContextMenuItemClicked); 
    }

}
private void ContextMenuItemClicked(CommandBarButton Ctrl, ref bool CancelDefault)
{
    if (currentExplorer.Selection.Count > 0)
    {
        object selObject = currentExplorer.Selection[1];
        if (selObject is MailItem)
        {
            // do your stuff with the selected message here
            MailItem mail = selObject as MailItem;
            MessageBox.Show("Message Subject: " + mail.Subject);
        }
    }
} 

我从AddButtonToNewDropdown()事件中调用ThisAddIn_Startup方法。

1 个答案:

答案 0 :(得分:4)

你需要在范围内使CommandBarButton成为一个类成员变量 - 否则它将被垃圾收集,并且事件不会像你观察到的那样触发。

public class ThisAddIn
{
   Office.CommandBarButton commandBarButton;

   private void AddButtonToNewDropdown()
   {
     // ...
   }
}

请参阅related SO post regarding similar issue