当我运行以下代码时,出现以下异常:
''# NOTE: ExcelApp is a Private main form variable
Dim ReportBooks As Excel.Workbooks = ExcelApp.Workbooks
Dim ReportBook As Excel.Workbook = ReportBooks.Open(localFilename)
Dim ReportSheet As Excel.Worksheet = ReportBook.Sheets("Report")
''# Retreive data from sheet
ReleaseCOM(ReportSheet)
ReportBook.Close(True) ''# Error raised here
ReleaseCOM(ReportBook)
ReleaseCOM(ReportBooks)
ERROR:
COMException was unhandled
The object invoked has disconnected from its clients.
(Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))
注意:所有数据似乎已正确检索。
请帮助我诊断并克服此错误。
最佳答案
RPC_DISCONNECTED ...可怕的是“调用的对象已与其客户端断开连接。”问题。造成这种情况的原因很多,看来您已经使用Excel.
解决了全局变量问题。您可以将第一个ReleaseCOM(ReportSheet)
放在ReportBook(Close)
下面并运行它吗?另外,请检出this。