C# – Pass Data into a VSTO Excel Workbook

cexcelvsto

I've got an VSTO Excel Workbook project that I'm using to gather information from a user. This workbook is being launched from inside of a host application, but I need to pass some parameters to the workbook before opening it so it knows what to display and how to display it. What's the best way to do this?

Best Answer

There is a way of passing data to a workbook which personally I don't really like, but maybe it can suit you. Basically, you set values for specific cells in the workbook, and then process those values in Excel's event handler:

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open(filepath);
                var sheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];
                var range = sheet.Range["A1"];
                range.Value2 = "some value";