How to get a global (public) reference to a custom Ribbon in my C# solution

472 views
Skip to first unread message

Ken

unread,
Sep 22, 2016, 7:23:05 PM9/22/16
to Excel-DNA
How do I get a public reference to the custom Ribbon so that I can access the Ribbon controls from another class in the same C# solution.

In MyRibbon.cs I have the following constructor. I don't instantiate an actual ribbon from this class since this is done by ExcelDna.  
So, then how do I get a reference to the ribbon that is created when the XLL loads so I can read the state of controls on the ribbon, like the state of a checkbox control?

[ComVisible(true)]
public class MyRibbon : ExcelRibbon
{

}

Govert van Drimmelen

unread,
Sep 23, 2016, 1:17:54 AM9/23/16
to exce...@googlegroups.com
Hi Ken,

You can track the MyRibbon instance by having a 'static' variable somewhere, which you set in the constructor of MyRibbon.

The actual state of the ribbon is not available to read as for a normal forms control - everything works with the callback functions that you put in the xml.
You can of course expose your internal values as properties for your other code, and update these from the callback. And even have properties that get set from your other code, which internally invalidates the ribbon, triggering the callback.
So the Office ribbon interface is a bit awkward, but you can wrap all of it in your class and exposing friendly interface. (This is exactly what VSTO does too.)

-Govert
--
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 https://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/d/optout.

Ken

unread,
Sep 23, 2016, 9:46:34 AM9/23/16
to Excel-DNA
Yes, I eventually figured this out.  I will post the code here for the benefit of others.

[ComVisible(true)]
public class MyRibbon: ExcelRibbon
{
public static MyRibbon TheRibbon; // allows us to access the Ribbon instance outside this class
internal bool flagMe;

public MyRibbon()
{
TheRibbon = this;
}
}

Then, in another class inside same project we can do something like this to access the internal flagMe :

public class SomeOtherClass
{
if (MyRibbon.TheRibbon.flagMe)
{

jcue

unread,
Nov 6, 2019, 3:00:47 AM11/6/19
to Excel-DNA
I know this is an old thread but is there an example of a wrapper class exposing a checkbox callback functions in a friendly interface as noted below?  I'm building a custom ribbon and I will probably have many checkboxes and I'm hoping to avoid an overly complex ribbon class.

Thank you

On Thursday, September 22, 2016 at 10:17:54 PM UTC-7, Govert van Drimmelen wrote:
Hi Ken,

You can track the MyRibbon instance by having a 'static' variable somewhere, which you set in the constructor of MyRibbon.

The actual state of the ribbon is not available to read as for a normal forms control - everything works with the callback functions that you put in the xml.
You can of course expose your internal values as properties for your other code, and update these from the callback. And even have properties that get set from your other code, which internally invalidates the ribbon, triggering the callback.
So the Office ribbon interface is a bit awkward, but you can wrap all of it in your class and exposing friendly interface. (This is exactly what VSTO does too.)

-Govert

On 23 Sep 2016, at 01:26, Ken <roe...@gmail.com> wrote:

How do I get a public reference to the custom Ribbon so that I can access the Ribbon controls from another class in the same C# solution.

In MyRibbon.cs I have the following constructor. I don't instantiate an actual ribbon from this class since this is done by ExcelDna.  
So, then how do I get a reference to the ribbon that is created when the XLL loads so I can read the state of controls on the ribbon, like the state of a checkbox control?

[ComVisible(true)]
public class MyRibbon : ExcelRibbon
{

}

--
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 exce...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages