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

Posted on 2009-02-23
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


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

    private OFFICE.CommandBarButton myButton = null;

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

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

    Author Closing Comment

    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

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    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.…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now