[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


How to define the OnAction event for a button placed on a worksheet?

Posted on 2009-02-23
Medium Priority
Last Modified: 2013-11-10
Hi all,

I'm beginning to work with VSTO and I've a very simple question to ask about controls placed in documents.

As you can see in the attached code, I've added a button on a worksheet, but I cannot handle the event in my function BtnTest_Click(), because Excel is looking for a VBA macro called "BtnTest_Click" in my workbook.

Is there an other way to work with controls in documents?
Am I forced to write a macro in VBA?

private void CreateButton()
            Excel.Worksheet sheet = Application.ActiveSheet as Excel.Worksheet;
            Excel.Button btn = (sheet.Buttons(oMissing) as Excel.Buttons).Add(10, 10, 50, 20);
            btn.Caption = "Test";
        private void BtnTest_Click()

Open in new window

Question by:JulienVan
LVL 14

Accepted Solution

Daniel Junges earned 1500 total points
ID: 23719996

here an sample for CommandBarButton, but seems that you need the solution from link above!

// interop.office.dll
private OFFICE.CommandBarButton myButton = null;

myButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ToolBarButtonClick);

private void ToolBarButtonClick(Office.CommandBarButton Ctrl, ref bool CancelDefault)

Author Closing Comment

ID: 31550148
Hi junges, thanks for the link, it's a good help, i'll just have to test it in C# for a common button on a document.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question