Sto provando a distribuire un'applicazione che funzioni correttamente sul mio PC di sviluppo e su alcune altre workstation. Tuttavia, alcuni utenti ricevono un errore che non riesco a capire.Interop C# Excel: Eccezione da HRESULT (DISP_E_BADINDEX)
Il programma è un'applicazione C# dotNet con funzionalità di Excel.Interop (Office 2003).
Mi sembra di avere un problema con "indici". La cosa strana è che questa parte funziona perfettamente su alcune macchine, ma genera un'eccezione fatale sugli altri ... Tutte le macchine sono di Windows 7 con Office 2003.
Questo è il codice corrispondente:
//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();
}
Questo è il log che sto ricevendo:
... 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
Questa è la versione di interoperabilità di Office di IndexOutOfRangeException. Il foglio di calcolo semplicemente non ha un secondo foglio. Oh foglio! –