Próbuję wdrożyć aplikację, która działa dobrze na moim komputerze programisty i niektórych innych stacji roboczych. Jednak niektórzy użytkownicy otrzymują błąd, którego nie mogę zrozumieć.C# Excel interop: Wyjątek od HRESULT (DISP_E_BADINDEX)
Program jest aplikacją C# dotNet z funkcją Excel.Interop (Office 2003).
Wygląda na to, że mam problem z "indeksami". Najdziwniejsze jest to, że ta część działa idealnie na niektórych maszynach, ale rzuca wyjątek krytyczny na innych ... Wszystkie maszyny są Windows 7 z Office 2003.
To jest odpowiedni kod:
//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();
}
ten jest dziennik, który otrzymuję:
... 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
To jest wersja interpolowa Office IndexOutOfRangeException. Arkusz kalkulacyjny po prostu nie ma drugiego arkusza. Och, arkusz! –