Outlook Event is not getting fired on click of custom button

不羁岁月 提交于 2019-12-11 10:23:27

问题


I am developing a Microsoft Outlook Add-in, where I have added one button in Add-In tab name OPENISMS. I could see the button, however on click the event is not getting fired. I have no clue why it is behaving in this manner. Please find below are code for adding button and attaching event to it. Any help will be highly appreciated.

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

I am calling AddButtonToNewDropdown() method from ThisAddIn_Startup event.


回答1:


You need to make the CommandBarButton a class-member variable in scope - otherwise it will be garbage collected and the event will not fire as you've observed.

public class ThisAddIn
{
   Office.CommandBarButton commandBarButton;

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

See related SO post regarding similar issue.



来源:https://stackoverflow.com/questions/14136188/outlook-event-is-not-getting-fired-on-click-of-custom-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!