简体   繁体   中英

Enable/Disable ribbon button based on MailItem subject (Outlook VSTO)

I have some trouble with a very simple VSTO AddIn. I added two custom Ribbons via Designer (Ribbon1 and Ribbon2) in order to create a custom button on two built-in ribbons ( Microsoft.Outlook.Mail.Read and Microsoft.Outlook.Explorer ). I would like to set the "Enabled" property of the ribbon buttons based on the selected mail's subject. It works nice in the Explorer window/ribbon, but i can't solve it in the Inspector window.

The problem is that Outlook generates Inspector's Ribbon only once, so when I open in the inspector window another mail, I cannot change the state of the button. I tried Invalidate() method and using Ribbon XML with getEnabled, but I don't succeed. What is the problem?

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

What is your code that invalidates the ribbon? What is your code that handles getEnabled callback?

You must explicitly call Invalidate() when the selection changes for the explorer. You must do the same every time Inspectors.NewInspector event fires for the items shown in an inspector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM