locked
Registering to OnClick event of ActiveX control RRS feed

  • Question

  • Unfortunately, I have not been able to figure out how to do the following:

    I am developing an Excel Add-In in VSTO / .NET / C#.

    I have been able to find out how to get references to the ActiveX controls in a worksheet. It is possible via the worksheet.OLEObjects collection.

    I can search that collection for a particular button if I know the name of the button. So far, so good.

    But now: How can I register an event handler that gets called when the button is clicked?

    I know how it works for Windows.Forms.Controls. You reference to Microsoft.Vbe.Interop.Forms.dll and use its namespace -> Microsoft.Vbe.Interop.Forms.
    Is there any equivalent for ActiveXControls?

    Thursday, August 25, 2016 2:51 PM

Answers

  • Hi,

    >>Is there any equivalent for ActiveXControls?

    How do you add the ActiveX control? What error do you get when registry ActiveXControls by Microsoft.Vbe.Interop.Forms.CommandButtonEvents_ClickEventHandler?

    The following code adds the ActiveX button at runtime and registers the button_click event. I am referring to Excel: How to run C# code behind with a click of a button on a Worksheet, without VBA code

    Add references: Microsoft.VisualBasic & Microsoft.Vbe.Interop.Forms

    using Excel = Microsoft.Office.Interop.Excel;
    using MSForms = Microsoft.Vbe.Interop.Forms;
    using Microsoft.VisualBasic.CompilerServices;
    
        private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Excel.Shape cmdButton = (Excel.Shape)Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddOLEObject("Forms.CommandButton.1",
                    Type.Missing, false, false, Type.Missing, Type.Missing, Type.Missing, 200, 100, 100, 100);
                cmdButton.Name = "btnClick";
                //In order to access the Command button object, we are using NewLateBinding class as below
                //(MSForms.CommandButton)Globals.ThisAddIn.Application.ActiveSheet.OLEObjects("CommandButton1");//
                MSForms.CommandButton CmdBtn = (MSForms.CommandButton)NewLateBinding.LateGet((Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet, null, "btnClick", new object[0], null, null, null);
                //Set the required properties for the command button
                CmdBtn.FontSize = 10;
                CmdBtn.FontBold = true;
                CmdBtn.Caption = "Click Me";
                //Wiring up the Click event
                CmdBtn.Click += new Microsoft.Vbe.Interop.Forms.CommandButtonEvents_ClickEventHandler(CmdBtn_Click);
    
            }
            void CmdBtn_Click()
            {
                //Adding the event code
                System.Windows.Forms.MessageBox.Show("I am called from C# VSTO add-in");
            }
    

    • Proposed as answer by Chenchen Li Monday, September 5, 2016 8:22 AM
    • Marked as answer by Chenchen Li Tuesday, September 6, 2016 10:20 AM
    Friday, August 26, 2016 8:53 AM

All replies

  • Hi,

    >>Is there any equivalent for ActiveXControls?

    How do you add the ActiveX control? What error do you get when registry ActiveXControls by Microsoft.Vbe.Interop.Forms.CommandButtonEvents_ClickEventHandler?

    The following code adds the ActiveX button at runtime and registers the button_click event. I am referring to Excel: How to run C# code behind with a click of a button on a Worksheet, without VBA code

    Add references: Microsoft.VisualBasic & Microsoft.Vbe.Interop.Forms

    using Excel = Microsoft.Office.Interop.Excel;
    using MSForms = Microsoft.Vbe.Interop.Forms;
    using Microsoft.VisualBasic.CompilerServices;
    
        private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Excel.Shape cmdButton = (Excel.Shape)Globals.ThisAddIn.Application.ActiveSheet.Shapes.AddOLEObject("Forms.CommandButton.1",
                    Type.Missing, false, false, Type.Missing, Type.Missing, Type.Missing, 200, 100, 100, 100);
                cmdButton.Name = "btnClick";
                //In order to access the Command button object, we are using NewLateBinding class as below
                //(MSForms.CommandButton)Globals.ThisAddIn.Application.ActiveSheet.OLEObjects("CommandButton1");//
                MSForms.CommandButton CmdBtn = (MSForms.CommandButton)NewLateBinding.LateGet((Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet, null, "btnClick", new object[0], null, null, null);
                //Set the required properties for the command button
                CmdBtn.FontSize = 10;
                CmdBtn.FontBold = true;
                CmdBtn.Caption = "Click Me";
                //Wiring up the Click event
                CmdBtn.Click += new Microsoft.Vbe.Interop.Forms.CommandButtonEvents_ClickEventHandler(CmdBtn_Click);
    
            }
            void CmdBtn_Click()
            {
                //Adding the event code
                System.Windows.Forms.MessageBox.Show("I am called from C# VSTO add-in");
            }
    

    • Proposed as answer by Chenchen Li Monday, September 5, 2016 8:22 AM
    • Marked as answer by Chenchen Li Tuesday, September 6, 2016 10:20 AM
    Friday, August 26, 2016 8:53 AM
  • thx for the reply but IMHO Microsoft.Vbe.Interop.Forms do not include ActiveX Controls 

    

    Friday, August 26, 2016 9:18 AM
  • Hi,

    What error do you get when registry ActiveXControls by Microsoft.Vbe.Interop.Forms.CommandButtonEvents_ClickEventHandler?

    To my knowledge, Forms.CommandButton.1 is an ActiveX button.

    In Using ActiveX Controls on Sheets, you could find:

    In Microsoft Excel, ActiveX controls are represented by OLEObject objects in the OLEObjects collection

    In the code above, by using AddOLEObject("Forms.CommandButton.1" , we are actually adding the ActiveX button and then register the click event for it.

    Monday, August 29, 2016 3:15 AM
  • Actually, that's exactly what the ActiveX controls are. These controls, originally designed for VBA UserForms can also be placed on an Office document's surface.

    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Wednesday, November 13, 2019 6:07 PM