我正试图部署一个在我的开发电脑和其他工作站上运行良好的应用程序。但是,一些用户会收到一个我无法理解的错误。
这个程序是一个带有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