Outlook Add-In – Enable/Disable Button during Runtime/from Code

add-inoutlookvsto

Initial Situation:

We are developing an Add-in for Outlook 2010 in C# with VS.NET 2010 based on Framework 4.0, VSTO, DevExpress WinForm Controls. In Designer we have a Ribbon with a RibbonTab, then a RibbonGroup then a RibbonButton. We're consuming WebServices from within this Outlook Add-in.

Objective:

We need to enable/disable the RibbonButtons when the WebService is available/unavailable (from/out of the code)

we've found the following links:

Links

Ribbon Object Model Overview: http://msdn.microsoft.com/en-us/library/bb608623.aspx
Ribbon Overview: http://msdn.microsoft.com/en-us/library/bb386097.aspx
Walkthrough: Updating the Controls on a Ribbon at Run Time: http://msdn.microsoft.com/en-us/library/bb608628.aspx

After hours of trying to figure out how to implement this we deciced to post/ask this question here on SO. Does anybody have a sample code? We tried the IRibbonExtensibility and CreateRibbonExtensibilityObject => we added the RibbonTab, Group and Button and added a subscription to the Click Event => The Event is fired but not handled (in button_Click(…) => System.Diagnostics.Debugger.Break() is not breaking the code execution)

Thanks!

Christian

Best Answer

You'll want to invalidate the Ribbon at a fairly frequent rate in order to refresh the visibility of each tab/button. You can do this by subscribing to the Click event (as you've done) and then calling RibbonObject.Invalidate();. Then add a getEnabled="yourTestFunction" parameter to each button, with public bool yourTestFunction(Office.IRibbonControl control) (Defined in the Ribbon.cs file) returning whether the web service is available or not.

Keep in mind if the web service is down, each click could hang your application for the amount of time you set on your timeout in the web service check

Edit:
Just realized the _Click event isn't mapped in the Excel COM library, so here's a bit of code that will run each time the cell selection is changed (not as frequent as every click, but hopefully good enough).

ThisAddIn.cs:

public static Excel.Application e_application;
public static Office.IRibbonUI e_ribbon;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        e_application = this.Application;
        e_application.SheetSelectionChange += new Excel.AppEvents_SheetSelectionChangeEventHandler(e_application_SheetSelectionChange);
    }

    void e_application_SheetSelectionChange(object Sh, Excel.Range Target)
    {
        e_ribbon.Invalidate();
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        e_application.SheetSelectionChange -= new Excel.AppEvents_SheetSelectionChangeEventHandler(e_application_SheetSelectionChange);
        e_application = null;
    }

Ribbon1.cs:

public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
    this.ribbon = ribbonUI;
    ThisAddIn.e_ribbon = ribbonUI; //Add this line
}

and

    public bool getEnabledTest(Office.IRibbonControl control)
    {
        //Whatever you use to test your Web Service
        //return false;
    }

Ribbon1.xml:

 <button id="WebService" label="Use The Web Service" onAction="executeWebService" getEnabled="getEnabledTest" />
Related Topic