我正在尝试部署一个在我的开发电脑和其他一些工作站上运行良好的应用程序。但是,一些用户收到我似乎无法理解的错误。
该程序是具有 Excel.Interop 功能的 C# dotNet 应用程序 (Office 2003)。
我似乎遇到了“索引”的问题。奇怪的是,这部分在某些机器上运行完美,但在其他机器上却抛出了一个致命的异常......所有机器都是 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