Reach ActiveX or Form objects (textbox) from codebehind in an Excel document

后端 未结 5 1487
别跟我提以往
别跟我提以往 2021-01-11 23:43

There are several textboxes in an excel file as ActiveX objects and I want to reach them from codebehind.

I am using ClosedXML for other fields, but I am open for ot

5条回答
  •  北海茫月
    2021-01-12 00:22

    For accessing OLE objects from C#, add reference to Microsoft Forms 2.0 object library. You can iterate through the controls for your desired checkbox and textbox. Enjoy !

    using Excel = Microsoft.Office.Interop.Excel;
    using VBE = Microsoft.Vbe.Interop.Forms;
    
    private static void ExcelOperation(string xlFileName)
            {
                var xlApp = new Excel.Application();
                var xlWorkbook = xlApp.Workbooks.Open(xlFileName);
                var xlSheet = xlWorkbook.Worksheets["your_sheet_Name"] as Excel.Worksheet;
    
                try
                {
                    Excel.OLEObjects oleObjects = xlSheet.OLEObjects() as Excel.OLEObjects;
                    foreach (Excel.OLEObject item in oleObjects)
                    {                   
                        if (item.progID == "Forms.TextBox.1")
                        {
                            VBE.TextBox xlTB = item.Object as VBE.TextBox;
                            Console.WriteLine("Name: " + item.Name);
                            Console.WriteLine("Text: " + xlTB.Text);
                            Console.WriteLine("Value: " + xlTB.get_Value());
                            Marshal.ReleaseComObject(xlTB); xlTB = null;
                        }
                        else if (item.progID == "Forms.CheckBox.1")
                        {
                            VBE.CheckBox xlCB = item.Object as VBE.CheckBox;
                            Console.WriteLine("checkbox: " + item.Name);
                            Console.WriteLine("Value: " + xlCB.get_Value());
                            Marshal.ReleaseComObject(xlCB); xlCB = null;
                        }                    
    
                    }
    
                    Marshal.ReleaseComObject(oleObjects); oleObjects = null;
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
    
                Marshal.ReleaseComObject(xlSheet); xlSheet = null;
                xlWorkbook.Close();
                Marshal.ReleaseComObject(xlWorkbook); xlWorkbook = null;
                Marshal.ReleaseComObject(xlApp); xlApp = null;
            }
    

提交回复
热议问题