Deactivating the ribbon button conditionally

862 views
Skip to first unread message

zohrehta...@gmail.com

unread,
Nov 27, 2014, 11:39:28 AM11/27/14
to exce...@googlegroups.com

Dear all/Govert,

I am using a ribbon button in ExcelDna. In the XML dna file I have the following:

<button id='ButtonPublish' label='Publish' image='Publish' size='large' onAction='RunTagMacro' tag='Publish' />

This is supposed to publish some value from an excel sheet. All working OK.

However the button is active all the time regardless of whether there is or isn’t any active workbook open.

I would like to update this so that when we open the excel if there is no workbook open, the button will be deactivated.

I have been struggling for the past hour and looked at some links, however I still don’t know how to do that.

I tried to add the GetEnabled callback as following. But then the button becomes disable immediately regardless of any “GetEnabled” function return.

<button id='ButtonPublish' label='Publish' image='Publish' size='large' onAction='RunTagMacro' tag='Publish' getEnabled ='GetEnabled'/>

I would really appreciate if anyone (or Govert) kindly can help with this.

Many thanks,

Zohreh

zohrehta...@gmail.com

unread,
Nov 27, 2014, 12:57:41 PM11/27/14
to exce...@googlegroups.com
An Example will be greatly appreciated. 
thanks,
zohreh

Govert van Drimmelen

unread,
Nov 28, 2014, 2:52:42 PM11/28/14
to exce...@googlegroups.com
What is the signature of the method you are you using for "GetEnabled"?

-Govert

zohrehta...@gmail.com

unread,
Dec 3, 2014, 9:30:06 AM12/3/14
to exce...@googlegroups.com
Hi Govert,
Thanks very much for the contact. I don't know how the signature should be. A few questions please: 

1. where does the function GetEnabled sit? In Ribbon class or Addin? 
2. What should it return? A boolean? or void?
3. Do I need to pass any control to the function? of IRibbonControl type? and why?
4. Ultimately where my condition as whether the button should be enabled or disabled will go? In the GetEnabled function? and a true/false return indicates if it will be either enabled or disabled?


If in Ribbon class I create a void function as following then my button gets disabled regardless of what the function doing.
public class Ribbon : ExcelRibbon
{
    public void GetEnabled(IRibbonControl control)
    {
            
    }
}
 
However if it returns a boolean like below, regardless of returning false or true it is never disabled!
public class Ribbon : ExcelRibbon
{
    public void GetEnabled(IRibbonControl control)
    {
        return true;  // or return false;
    }
}

i would be very grateful if you could help with an example please.
Many thanks
Zohreh

Govert van Drimmelen

unread,
Dec 3, 2014, 4:49:16 PM12/3/14
to exce...@googlegroups.com
Hi Zohreh,

The GetEnabled method would be in your ExcelRibbon-derived class.

I look here: http://msdn.microsoft.com/en-us/library/aa722523(v=office.12).aspx  , and find that the C# signature for getEnabled is:

bool GetEnabled(IRibbonControl control)


-Govert


From: exce...@googlegroups.com [exce...@googlegroups.com] on behalf of zohrehta...@gmail.com [zohrehta...@gmail.com]
Sent: 03 December 2014 04:30 PM
To: exce...@googlegroups.com
Subject: [ExcelDna] Re: Deactivating the ribbon button conditionally

--
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.

Mike Sullivan

unread,
Dec 3, 2014, 7:11:37 PM12/3/14
to exce...@googlegroups.com
I had similar confusion because the docs aren't really very good at explaining the WHOLE solution...just bits of it, and they don't explain how it all comes together. It is actually really simple.

The addin is defined as 

    public class addinName : ExcelRibbon, IExcelAddIn


And in that class, you have a GetCustomUI method in which you are building an XML string for the ribbon elements. In those elements that you want to be enabled/disabled, you add the parameter "getEnabled" and set it to your GetEnabled method (mine is called "ribbonGetEnabled").  That method returns a true or false.  For my GetCustomUI, I have a button defined as:
       
 public override string GetCustomUI(string uiName)
 {
        StringBuilder ribbonXml = new StringBuilder();
        [...]
        ribbonXml.AppendLine(@"  <button id='...' getEnabled='ribbonGetEnabled' imageMso='...' label='...' ...);
        [...]
       return ribbonXml.ToString();
 }

and then I have

        public bool ribbonGetEnabled(IRibbonControl control)
        {
            if (...)
                return false;
            return true;
        }


Mike
Reply all
Reply to author
Forward
0 new messages