Execute programmatically a ribbon button from C# code

840 views
Skip to first unread message

Hamza Nouri

unread,
Jun 14, 2016, 6:57:01 AM6/14/16
to Excel-DNA
Hello,
My question is as follow : can we execute a ribbon button programmatically from c# code, meaning simulate the click on that button?
Thanks.
Best regards.
Hamza

Govert van Drimmelen

unread,
Jun 14, 2016, 7:22:09 AM6/14/16
to exce...@googlegroups.com

Hi Hamza,

 

Do you want to do this inside your add-in, or from outside while automating Excel?

 

The most ‘correct’ option is probably to use UI Automation to properly simulate the button press.

The UI Automation learning curve is quite steep though. If you’re automating Excel, a tool like “White” can help (https://github.com/TestStack/White).

 

If you’re trying to do this from inside the add-in, it’s probably better to just call your own method internally.

 

Suppose you keep track of the ribbon instance, by storing to a static field when it is constructed:

 

public class MyRibbon : ExcelRibbon

{

    public static MyRibbon TheOneRibbon;

    public MyRibbon

    {

        TheOneRibbon = this;

    }

 

    public void OnBigButtonPressed(IRibbonControl button) {…}

}

 

Then you can call this from other code as:

 

    MyRibbon.TheOneRibbon.OnBigButtonPressed(fakeRibbonControl).

 

The work for you is then just to  implements IRibbonControl in your own class, and you only have to do it well enough to make your OnBigButtonPressed method happy.

 

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

Hamza Nouri

unread,
Jun 14, 2016, 7:43:26 AM6/14/16
to Excel-DNA
Hi Govert,
Thanks a lot for your quick response.
In fact i do want to execute the button from inside the add-in, so i followed your steps but the last think to do is to 
implement the IRibbonControl in order to make the execution correct. Could you please guide me on how getting the right control to pass it to the method. 
Thanks in advance.
BestRegatds

Govert van Drimmelen

unread,
Jun 14, 2016, 7:57:41 AM6/14/16
to exce...@googlegroups.com

I don’t know how you would get hold of the actual control object.

For this purpose you can send in a fake control that implements the interface:

 

class FakeControl : IRibbonControl

{

    public object Context { get; private set; }

    public string Id { get; private set; }

    public string Tag { get; private set; }

    public FakeControl(string id, string tag)

    {

        Id = id;

        Tag = tag;

    }

}

 

I don’t know of a way to ask Excel for the Id or Tag of a button, so you have to manage this information yourself.

 

-Govert

 

 

From: exce...@googlegroups.com [mailto:exce...@googlegroups.com] On Behalf Of Hamza Nouri


Sent: 14 June 2016 13:43
To: Excel-DNA <exce...@googlegroups.com>

--

Hamza Nouri

unread,
Jun 14, 2016, 8:14:22 AM6/14/16
to Excel-DNA
Thanks again Govert, i'll do this.
Have a good day :)
Best regards.
Hamza

Le mardi 14 juin 2016 12:57:01 UTC+2, Hamza Nouri a écrit :
Reply all
Reply to author
Forward
0 new messages