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