【问题标题】:Outlook Event is not getting fired on click of custom button单击自定义按钮时未触发 Outlook 事件
【发布时间】:2012-12-17 15:42:47
【问题描述】:

我正在开发一个 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);
        }
    }
} 

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

【问题讨论】:

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


    【解决方案1】:

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

    public class ThisAddIn
    {
       Office.CommandBarButton commandBarButton;
    
       private void AddButtonToNewDropdown()
       {
         // ...
       }
    }
    

    related SO post regarding similar issue

    【讨论】:

    • 太棒了 - 很高兴它帮助了你。请将此标记为已接受的答案,以便其他人受益。只需单击投票箭头下方左侧的对勾(复选标记)。
    猜你喜欢
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 2015-10-18
    • 2014-05-04
    • 2010-10-21
    相关资源
    最近更新 更多