How to disable Ribbon button from Addin class

2,200 views
Skip to first unread message

ninjamayo

unread,
Jun 7, 2014, 9:58:47 AM6/7/14
to exce...@googlegroups.com
Hi

I have created an Addin class inheriting from IExcelAddIn. I also have a Ribbon created using the Ribbon.xml. It all works fine but every time I close a workbook I want to disable the button in my Ribbon. I have an event handler for the Workbook Close event but I have no idea how to access the Ribbon buttons and disable them.

Any ideas?

Dimitris

Govert van Drimmelen

unread,
Jun 7, 2014, 10:08:46 AM6/7/14
to exce...@googlegroups.com
Hi Dimitris,

Working with the ribbon is a bit awkward - it's one place where VSTO has some nice wrapper class that make things a bit easier.

You basically have to:
1. Set up an onLoad handler for your customUI, and grab and store the IRibbonUI interface that you get there.
2. Set up a getEnabled handler for your button.
3. Call InvalidateControl (or Invalidate) on the IRibbonUI when you want to have Excel call the getEnabled handler.

If you get stuck, let me know and I'll try to make an example.

Cheers,
Govert



From: exce...@googlegroups.com [exce...@googlegroups.com] on behalf of ninjamayo [dtra...@gmail.com]
Sent: 07 June 2014 03:58 PM
To: exce...@googlegroups.com
Subject: [ExcelDna] How to disable Ribbon button from Addin class

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at http://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.

ninjamayo

unread,
Jun 7, 2014, 10:25:59 AM6/7/14
to exce...@googlegroups.com
Hi Govert,

thanks again for replying. I added the onLoad event handler and I get in a static variable the ribbon interface. I am struggling a little bit with the getEnabled event handler. I added one in one of my buttons like this:

<button id="btnHelp" getEnabled="btn_GetEnabled" onAction="btnHelp_Click" ...

and in the code

        public void btn_GetEnabled(IRibbonControl control)
        {
            
        }

        public static void InvalidateHelpButton()
        {
            myRibbon.InvalidateControl("btnHelp");
        }

How do I now enable or disable this button?

An example would definitely help.


Regards
Dimitris

Govert van Drimmelen

unread,
Jun 7, 2014, 10:48:46 AM6/7/14
to exce...@googlegroups.com
Hi Dimitris,


Check the signature for your callbacks against the list here: http://msdn.microsoft.com/en-us/library/aa722523(v=office.12).aspx 
For getEnabled I see this entry: 
    C#: bool GetEnabled(IRibbonControl control)

So you'd have:
        // This is the callback handler (name and signature must match the getEnabled="btn_GetEnabled" in the customUI XML)
public bool btn_GetEnabled(IRibbonControl control)
        {
            return _btnIsEnabled;
        }

// This is internal bookkeeping
bool _btnIsEnabled;
        internal void SetBtnEnabled(bool isEnabled)
{
_btnIsEnabled = isEnabled;
  InvalidateHelpButton();
}

Of course you might factor and wrap this better if you'll be doing a lot of it. The VSTO wrapper classes are an example of a higher-level wrapper around this Ribbon API.

-Govert




Sent: 07 June 2014 04:25 PM
To: exce...@googlegroups.com
Subject: Re: [ExcelDna] How to disable Ribbon button from Addin class

--

ninjamayo

unread,
Jun 7, 2014, 11:06:56 AM6/7/14
to exce...@googlegroups.com
Hi Govert,

with your help it all works. Thanks so much for your really fast response and for the great help. Much appreciated.


Regards
Dimitris
Reply all
Reply to author
Forward
0 new messages