我想部署一個應用程序,可以在我的開發電腦和其他工作站上正常工作。 但是,有些用戶收到我無法理解的錯誤。C#Excel互操作:從HRESULT異常(DISP_E_BADINDEX)
該程序是一個具有Excel.Interop功能(Office 2003)的C#dotNet應用程序。
我似乎遇到'索引'的問題。奇怪的是,這部分完美的作品在一些機器上,但引發他人致命異常... 所有的機器都是Windows 7與Office 2003
這是相關代碼:
//Change sheet code (index is 1, 2, 3) -> errors at #2
public void ChangeWorksheet(int sheetIndex)
{
if (_OnXLSEvent != null) _OnXLSEvent(string.Format("TEMP: working on page {0}", sheetIndex));
_WS = _WSs[sheetIndex];
_Shapes = _WS.Shapes;
_PageSetup = _WS.PageSetup;
if (_OnXLSEvent != null) _OnXLSEvent(string.Format("TEMP: working on page {0}", _WS.Name));
}
//Constructor (_App and _WBs are static)
public ExcelProcessor(bool SaveAutomatically = false, string SavePath = "")
{
if (_App == null)
_App = new XLS.Application();
if (_WBs == null)
_WBs = _App.Workbooks;
_WB = _WBs.Add();
_WSs = _WB.Sheets;
_WS = _WSs[1];
_Shapes = _WS.Shapes;
_PageSetup = _WS.PageSetup;
_SavePath = SavePath;
_SaveOnDispose = SaveAutomatically;
_App.DisplayAlerts = false;
ApplyPageSetup();
}
這是我收到的日誌:
... Irrelevant
8:52: TEMP: working on page 1
8:52: TEMP: working on page Sheet1
8:52: TEMP: working on page 2
8:52: Error occurred:
Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
at Microsoft.Office.Interop.Excel.Sheets.get__Default(Object Index)
at Classes.XLSInterop.ExcelProcessor.ChangeWorksheet(Int32 sheetIndex) in c:\Users\panjaj\Documents\VS Projects\Projects\Client Projects\ProFormaCreator\ProFormaCreator\Classes\XLSInterop\ExcelProcessor.cs:line 74
at Classes.ApplicationManager.Manager.ProcessSingleDocument(InFileDocument doc) in c:\Users\panjaj\Documents\VS Projects\Projects\Client Projects\ProFormaCreator\ProFormaCreator\Classes\ApplicationManager\ApplicationManager.cs:line 327
at Classes.ApplicationManager.Manager.ConvertFile(String File) in c:\Users\panjaj\Documents\VS Projects\Projects\Client Projects\ProFormaCreator\ProFormaCreator\Classes\ApplicationManager\ApplicationManager.cs:line 172
這是Office互操作版本的IndexOutOfRangeException。電子表格根本沒有第二張紙。哦,表! –