Home » MS OfficeRSS

VSTO Capture Worksheet Button Click Event

Is it possible to caputre the click event of an Excel worksheet button click event. I have visual studio 2010 and have a working excel 2010 addin created using visual basic.

I have search the web and found many exapmles on adding worksheet controls at design or run-time but have been unsuccessful on finding an example of how the click action of the controls in handled.


2 Answers Found


Answer 1

There are two different type Buttons

Oleobjects Forms

Since oleobjects aren't  suppose to be used in new designs I will on use the Forms button.  the capture of the forms button can be assigned to any macro.  To assign the button to the macro use htis code

This is VBA.

   Set myBut = Sheets("Sheet1").Buttons.Add(332.25, 130.5, 6, 6)
   myBut.OnAction = "Macro1"

Rather than add buttons and macros to your Visual Studio code you may want to create a template Excel Workbook and use the template to create you new excel workbook rather than start from scratch.

You would need to add a macro into the excel workbook.  To add a macro into an excel workbook you need to create a "*.BAS" Text file and import the text file into the workbook.  The best way of creating the BAS file is to create a macro manually into a workbook and then export the file.

I would look at Chip Pearson's Webiste on the code to exporting and import the BAS file.  See webpage.  Chips code is in VBA ode but can easily be converted to C++, C#, VBnet.  Make sure you add the reference below (even in C# or C++ becauwe you are adding a VBA macro to the workbook)

Microsoft Visual Basic For Applications Extensibility 5.3.






Answer 2

Hi Chad

Can you explain in a bit more detail what it is you're after? I get the impression you want to interact with a VSTO workbook from your add-in (COM or VSTO?)? Is this a VSTO workbook you (or your company) have the source code for, or does it "belong" to a third-party?


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter