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?

Thanks,
Julien
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";
            btn.OnAction="BtnTest_Click";
        }
        private void BtnTest_Click()
        {
            MessageBox.Show("test");
        }

Open in new window

LVL 2
JulienVanAsked:
Who is Participating?
 
Daniel JungesCommented:
see:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_23584338.html


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)
{
}
0
 
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.
0
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.