m_collard
m_collard

Reputation: 2028

How to click a Excel ribbon button from code behind

We use a 3rd party Excel Addin to populate data in an Excel worksheet. I've been given the task of writing a C# app (WinForms or VSTO) that will automatically click the Ribbon button and process the data.

The problem is I can't find any way to automate clicking the Ribbon button (on the Add-ins tab) using C# code.

I've also tried using Win32 API's to send key presses to the parent Excel window (using SendMessage), but the Key presses weren't received. (I checked using Spy++). And looked at the public methods exposed in the Add-in assembly, but it didn't contain any public methods to do the same as clicking the ribbon button.

Does anyone know how I can click a ribbon button in the Excel (add-ins) ribbon using C# code?

Upvotes: 4

Views: 2426

Answers (1)

Mitja Bezenšek
Mitja Bezenšek

Reputation: 2533

Look at the SendKeys method of the Excel Application class. It allows you to send the keys directly to Excel. You can press the Alt key to see which shortcuts are assigned to which buttons / ribbon tabs. I automated some tests using this and it worked fine.

If you needed even more complex things you could also look into Sikuli. It's based on image recognition and is using Java.Robots class for sending keypresses and clicks. Automating UI tasks is quite easy with it.

Upvotes: 3

Related Questions