excel互操作exception0x8002000B

我有以下代码来打开工作簿,并保留一张表:

//Get a new workbook. oWB = app.Workbooks.Add(Missing.Value); int len = oWB.Sheets.Count; for (int i = 1; i < len; i++) { ((Worksheet)oWB.Sheets[i]).Delete(); } oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.ActiveSheet; 

这个代码在许多客户端configuration中工作了很多年。

出于某种原因,我有一个客户与Excel 2013(在希伯来语),在他的机器上我得到以下例外:

 System.Runtime.InteropServices.COMException (0x8002000B): Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) at Microsoft.Office.Interop.Excel.Sheets.get__Default(Object Index) at IOSIGHT.Win.ActiveReports.ExcelHelper.StartExcel() 

如果我了解情况,似乎由于某种原因,工作簿中没有任何工作表,所以对oWb.ActiveSheet的调用将引发exception。 任何想法是什么造成这个?

是的,我看到了同样的例外其他职位,但我的代码不符合他们的情况。

感谢您的帮助,奥默

向后循环,所以你不要尝试删除不再存在的索引。

例如for (int i = len; i > 1; i--) ...