C# Excel interop: Exception de HRESULT (DISP_E_BADINDEX)

Je suis en train de déployer une application qui fonctionne très bien sur mon pc de développement et de certains autres postes de travail.
Cependant, certains utilisateurs reçoivent un message d'erreur que je n'arrive pas à saisir.

Le programme C# dotNet app avec Excel.Interop fonctionnalité (Office 2003).

Je semble avoir un problème avec 'index'. La chose étrange est que cette partie fonctionne parfaitement sur certaines machines, mais jette une exception irrécupérable sur les autres...
Toutes les machines sont sous Windows 7 avec Office 2003.

C'est le code correspondant:

//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();
}

C'est le journal que je reçois:

... 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
C'est le Bureau de l'interopérabilité version de IndexOutOfRangeException. La feuille de calcul n'a tout simplement pas avoir une 2ème feuille. Oh feuille!

OriginalL'auteur Recipe | 2013-05-31