Editing a Textbox and CheckBox in Excel file using Interop
-
Hi, I am using Interop to gain access and make editions to an excel file and I am able to make it to individual cells. However I cannot seem to figure out how to make changes to an object like a textbox and checkbox programmatically using C# in the Excel file.
-
Hi, I am using Interop to gain access and make editions to an excel file and I am able to make it to individual cells. However I cannot seem to figure out how to make changes to an object like a textbox and checkbox programmatically using C# in the Excel file.
I assume here that you can get the Excel, Workbook and Worksheet objects... String sFileName = <full path to file>; Microsoft.Office.Interop.Excel.Application oApp = new Microsoft.Office.Interop.Excel.Application(); Workbook wkb = oApp.Workbooks.Open(sFileName, .....); Worksheet sheet1 = wkb.Worksheets["Sheet 1"] as Worksheet; Microsoft.Office.Interop.Excel.CheckBox cb = sheet1.CheckBoxes("Check Box 101"); ...the rest should be obvious. For example, cb.Value = true; That's the code. The non-obvious part to me was the name - in this case "Check Box 101". In the upper left corner of Excel, as you click from cell to cell, it shows the name of the cell. For example, A1, A2, etc... If you RMB on the CheckBox it will show the name of the CheckBox in this very same location. You can change the name to whatever you want. That's the name you use in code to access the CheckBox from the list. Excel will auto-name them so there is some predictability. But I've received spreadsheets where the names are all over the map. Good luck!