This project has moved and is read-only. For the latest updates, please go here.

XML enabled tag in Excel

Jun 10, 2014 at 12:44 AM
Hi folks,

Does anyone know how to leverage the enabled="false"/"true" XML tag at runtime?

Or maybe a better question for the group, how do you toggle enabling/disabling buttons in an excel ribbon? Basic functionality would be when I am logged in the buttons are enabled, and when logged out, the other buttons are disabled.

Sample XML Ribbon for Excel:
<?xml version="1.0" encoding="utf-8" ?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoadRibbonUI">
<ribbon>
<tabs>
  <tab id="My.Addin" label="My Addin">
    <group id="group1" label="My Addin">
      <button id="aboutButton" label="About" onAction="OnClickAboutButton" />
      <button id="settingsButton" label="Settings" onAction="OnClickSettingsButton" /> 
      <button id="loginButton" label="Login" onAction="OnClickLoginButton" /> 
      <separator  id="Separator1" />
      <button id="myButton" enabled ="false" label="My Button" imageMso="CreateReportFromWizard" size="large" onAction="OnClickMyButton" />
      <button id="myOtherButton" enabled ="false" label="My Other Button" imageMso="DataRefreshAll" size="large" onAction="OnClickMyOtherButton" />
    </group>
  </tab>
</tabs>
</ribbon>
</customUI>


Thanks!!
Jun 10, 2014 at 11:50 PM
Figured it out..

In XML:
You don't use the enabled= tag, you use the callback tag getEnabled=

<button id="myButton" label="My Button" imageMso="CreateReportFromWizard" size="large" onAction="OnClickMyButton" getEnabled="getMyButtonEnabled" />

In C#:
    // our ribbon instance to manipulate ui at runtime 
    private Office.IRibbonUI RibbonUI { get; set; }

     // defined in RibbonUI.xml to get a instance for ouer ribbon ui.
    public void OnLoadRibbonUI(Office.IRibbonUI ribbonUI)
    {
        RibbonUI = ribbonUI;
    }

    // defined in RibbonUI.xml to get MyButton state, and to set via return value
    public bool getReportsEnabled(Office.IRibbonControl control)
    {
        RibbonUI.InvalidateControl(control.Id);     //Must call InvalidateControl method on the button to force a refresh of it's state, set below

        return false;                // RETURN false TO DISABLE, RETURN true TO ENABLE.
    }

Since everything is in callbacks, you initially grab the RibbonUI for later use.
Then you use the getEnabled tag to define the callback routine onload and when updated later. By passing true or false at callback time it sets the control enabled or disabled accordingly.
Last you call InvalidateControl on the button you just fiddled with, to refresh the UI state.

Still perfecting my code, but hope this thread helps other NOOBS like me getting buttons to work.

Enjoy!