Problem trigger Excel ActiveX button from another Windows Forms App

Piyaphruk 21 Reputation points
2021-08-29T08:53:14.083+00:00

I receipt requirement about autorun Excel macro program but I familiar only Windows Forms App. I have tried Microsoft.Office.Interop.Excel to activate buttons. Its not works.

public bool openFileExcel(string filename)
        {
            Excel.Application _xlsm = new Excel.Application();
            Excel.Workbook _xlsB = _xlsm.Workbooks.Open(_fPath + filename);
            _xlsm.Visible = true;
            Excel.Worksheet _xlsS = _xlsB.Worksheets["Control"];
            Excel.OLEObject _oObj = _xlsS.OLEObjects("m_btnReset");

            _xlsm.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
            if (_xlsm.Ready)
            {
                try
                {
                    _oObj.Select();
                    _oObj.Activate();
                }
                catch(Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                return false;
            }
            else
            {
                return false;
            }
        }

Please guiding me.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,834 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,508 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2021-08-30T08:38:51.657+00:00

    @Piyaphruk , based on my test, you could try to use the Property _oObj.Object.Value = true ; to trigger the excel activex Button from Winform app.

    Here is a code example you could refer to.

    private void button1_Click(object sender, EventArgs e)  
            {  
                string xlFileName = Path.Combine("D:\\", "test1.xlsm");  
                var xlApp = new Excel.Application();  
                var xlWorkbook = xlApp.Workbooks.Open(xlFileName);  
                var xlSheet = xlWorkbook.Worksheets["Sheet1"] as Excel.Worksheet;  
                Excel.OLEObject _oObj = xlSheet.OLEObjects("CommandButton1");  
               _oObj.Object.Value = true ;     // This code will trigger the button  
                xlWorkbook.Save();  
                xlWorkbook.Close();  
      
                Marshal.ReleaseComObject(xlWorkbook); xlWorkbook = null;  
                Marshal.ReleaseComObject(xlApp); xlApp = null;  
      
      
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful