Excel Ribbon select event

196 views
Skip to first unread message

Andrey Eremenko

unread,
May 10, 2021, 10:41:19 PM5/10/21
to Excel-DNA
Hi all,

Is there a way to run a function when a custom ribbon is activated (clicked) by the user?

Thanks.

Govert van Drimmelen

unread,
May 11, 2021, 4:01:29 AM5/11/21
to exce...@googlegroups.com
Hi Andrey,

Could you expand a bit on your question?
Do you mean a function that is on the worksheet, or a function you have defined in the add-in?
Do you just want to call the function and get the result for further processing in the ribbon handler?
For your own UDFs you can just make the .NET call directly:

    var myResult = MyFunctions.DoTheMath(123, 456);

For built-in function you might use the Excel Application object:

    dynamic xlApp = ExcelDnaUtil.Application;
    var myResult = xlApp.WorksheetFunction.ACos(0);

But maybe you mean something else...?

-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 view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/8814dca2-c580-4335-93e4-8ffbfb7bb142n%40googlegroups.com.

Andrey Eremenko

unread,
May 18, 2021, 12:00:54 AM5/18/21
to Excel-DNA
Hi Govert,

I meant a C# method which can be triggered by switching to my custom ribbon. For example, the user may have HOME tab active, but when he switches to a custom ribbon, I want to run a method in C#. From what I know, Excel doesn't have events for changing tabs. 

Thanks.

Govert van Drimmelen

unread,
May 18, 2021, 5:55:39 AM5/18/21
to exce...@googlegroups.com
--------------------------------------------------
Excel-DNA is now registered on GitHub Sponsors.
Easily add Excel-DNA support to your private or corporate GitHub account.
--------------------------------------------------

Hi Andrey,

As you point out, the normal ribbon interface does not have an event or similar to let you know when the active tab changes.
I think it is possible to monitor this with the accessibility interfaces - see https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb404170(v=office.12).
These interfaces can be a bit fiddly to get working, so let me know if you want some help trying to figure it out.

-Govert



Reply all
Reply to author
Forward
0 new messages