How to call procedure from VBA or triggered by a key event

28 views
Skip to first unread message

Albrecht Hilmes

unread,
Jul 9, 2025, 5:01:02 AMJul 9
to Excel-DNA

Is it possible to start a "ribbon-procedure" (void DoSomething(IRibbonControl control)) also directly from excel?

e.g. in an VBA-sub or with "start macro"?

Or is it possible to react to a keystroke (e.g. F5)?

Albrecht Hilmes

unread,
Jul 9, 2025, 5:01:58 AMJul 9
to Excel-DNA
Govert wrote:

Hi Albrecht,

 

Please start a new discussion for new questions – that helps future me and others find the relevant topic more easily.

 

It’s not easy to call the ribbon procedure from VBA.

But you can define and register macro commands in your add-in like this

        [ExcelCommand(ShortCut ="{F5}")]   // Or something like "^w" to mean Ctrl + w

        public static void DoTheWork()

        {

            var app = ExcelDnaUtil.Application as Application;

            app.ActiveSheet.Range["A1"].Value = "Hello from a command!";

        }

 

Such a macro can be:

  • Run from VBA with Application.Run("DoTheWork") and
  • Called directly from other code in your add-in (as a normal C# method), in particular from the ribbon callback too.
  • You can also type the name of the macro into the “Macros” dialog box that you get from pressing Alt + F8 – the macro command is not listed there since macros from xll add-ins are always considered “hidden”. But when you type the full name, the “Run” button becomes enabled and you can execute the macro.
  • Set as the ‘Assign Macro’ for a button that you insert on a sheet.

 

You can directly add a shortcut to the ExcelCommand definition, as above, but sometimes you cannot override the built-in shortcuts this way, so "^c" (Ctrl + C) won’t work.

 

-Govert

Reply all
Reply to author
Forward
0 new messages