【问题标题】:Enable/Disable ribbon button based on MailItem subject (Outlook VSTO)基于 MailItem 主题启用/禁用功能区按钮 (Outlook VSTO)
【发布时间】:2019-09-03 09:39:08
【问题描述】:

我在使用非常简单的 VSTO 插件时遇到了一些问题。 我通过 Designer(Ribbon1 和 Ribbon2)添加了两个自定义功能区,以便在两个内置功能区(Microsoft.Outlook.Mail.ReadMicrosoft.Outlook.Explorer)上创建自定义按钮。 我想根据所选邮件的主题设置功能区按钮的“启用”属性。 它在资源管理器窗口/功能区中运行良好,但我无法在检查器窗口中解决它。

问题是 Outlook 只生成一次检查员功能区,所以当我在检查员窗口中打开另一封邮件时,我无法更改按钮的状态。我尝试了 Invalidate() 方法并将 Ribbon XML 与 getEnabled 一起使用,但我没有成功。有什么问题?

namespace ApproveReport
{
    public partial class ThisAddIn
    {
        // Explorer object
        Outlook.Explorer ThisExplorer;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            // SelectionChange Event
            ThisExplorer = Globals.ThisAddIn.Application.ActiveExplorer();
            ThisExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(ThisExplorer_SelectionChange);
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Empty
        }

        public void ThisExplorer_SelectionChange()
        {
            if(ThisExplorer.Selection.Count == 1)
            {
                Outlook.MailItem OriginalMessage = ThisExplorer.Selection[1];

                if (OriginalMessage.Subject.Contains("Report"))
                {
                    Globals.Ribbons.Ribbon1.buttonApproveReport.Enabled = true;
                    Globals.Ribbons.Ribbon2.buttonApproveReport.Enabled = true;
                }
                else
                {
                    Globals.Ribbons.Ribbon1.buttonApproveReport.Enabled = false;
                    Globals.Ribbons.Ribbon2.buttonApproveReport.Enabled = false;
                }
            }
         }
      }
  }

【问题讨论】:

    标签: c# outlook vsto outlook-addin office-addins


    【解决方案1】:

    使功能区无效的代码是什么?你处理 getEnabled 回调的代码是什么?

    当资源管理器的选择发生变化时,您必须显式调用 Invalidate()。每次为检查器中显示的项目触发 Inspectors.NewInspector 事件时,您都必须这样做。

    【讨论】:

    • 我用 Globals.Ribbons.Ribbon1.RibbonUI.Invalidate(); 行添加了一个 NewInspector 事件。这是我事件的第一行,之后我尝试设置按钮的启用属性。代码运行正常(我用 MessageBoxes 测试过),但启用方法对我的按钮没有任何影响!我应该怎么办?我没有成功使用 getEnabled 实现 XML Ribbon 解决方案...
    • 再次,发布显示 getEnabled 和其他方法的代码。
    • 根据您的回复我设法解决了它,您建议的事件处理程序是关键......所以事件的正确顺序:1.)NewInspector 事件,2.)检查 MailItem 主题,将启用/禁用状态存储在布尔变量中,3.)使功能区无效,4.)通过 getEnabled 运行启用操作,将布尔值设置为按钮状态。再次感谢!
    • @DmitryStreblechenko 我有一个关于VSTO AddIn 项目WORD 的问题here。我想知道您是否有时间发表评论/建议等。
    猜你喜欢
    • 1970-01-01
    • 2014-07-15
    • 2011-09-15
    • 1970-01-01
    • 2011-10-12
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多