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

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

Who is Participating?
Daniel JungesCommented:

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)
JulienVanAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.