I want to pass an excel workbook/sheet as a reference between several functions in order to do different operations on it. I have been looking for hours and can't find out how to do this. Is this not common practice to have multiple functions work on an excel sheet in this way? the below code doesn't work ..doesn't work if i pass by value either.
'System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800401A8' '
pass_ref(ref xlWorkSheet1); //call this
void pass_ref(ref Excel.Worksheet sheet) {
MessageBox.Show("Pass Ref:" + sheet.Name); //can't produce sheet name
}
xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
pass_ref(xlWorkSheet1);
xlWorkBook.Close(true, misValue, misValue);
Found a workaround. Save the workbook before trying to pass the sheet off. This worked.
There are 3 basic ways to deal with Office Formats, depending on wich ones you need to support. And another one altogether might be better:
I always advise going for the OpenXML SDK/Option 1. Rather limit the supported Formats then going back to the COM way. OleDB would be my second bet, despite not knowing the details. COM is best left as a fallback solution if nothing else works.
User contributions licensed under CC BY-SA 3.0