I am playing around with Excel Interop in .NET where I am encountering COMException over "Excel._WorkSheet.Copy(Type.Missing,Type.Missing)".
The behavior is very strange : I am copying worksheets from a template based .xls and generating a new workbook with specified worksheets( probably selected by user from GUI ). As soon as it encounters Excel._Worksheet WorkSheet.Copy(Type.Missing,Type.Missing), it throws following COMException.
"A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred.
Additional information: Exception from HRESULT: 0x800A03EC"
After experimenting with different way, I copied manually specified worksheet to another newly created instance of Excel and given this workbook as source to the same code. Here, it worked fine! It created an exact copy by opening a new excel instance and I had no exception at all.
I don't know why this behavior is occuring? Is it related to the contents inside that worksheet, If so, same and exact worksheet is being copied to another newly created blank worksheet and everything worked fine.
I have to produce reports for the specified worksheets given by the user, So I have to open the templated workbook from where, I need to copy those specified worksheets and later those worksheets would be filled in with the specified data.
I am using the following code :
Excel._Worksheet WorkSheet = null;
Excel.Application excel = new Excel.Application();
excel.Visible = true;
excel.DisplayAlerts = false;
_ExcelWorkbook = excel.Workbooks.Open(a_WorkbookName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,true);
WorkSheet = (Excel.Worksheet)_ExcelWorkbook.Sheets[1];
WorkSheet.Copy(Type.Missing, Type.Missing); // throws COM Exception of 0x800A03EC.
Regards Usman