3

I have the following object that later in my code I want to dispose() IF it exists. Is there a way to find if it exists?

Public objExcel As Excel.Application

The best I could come up with is to put the disposal into a try...catch block like this:

Try
   objExcel.dispose()
Catch ex As Exception
   'do nothing
End Try

Does anyone have a more elegant, less kludgy method?

4

2 回答 2

2

要完全处理 Excel 对象,您必须使用 marshal 类。System.Runtime.InteropServices.Marshal. 您还需要以相反的顺序释放所有 excel 对象 - 工作表、工作簿、excel 对象。如果没有,您可以在任务管理器中查看 Excel 进程。

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obgExcel)
于 2013-10-17T16:58:59.167 回答
1

首先检查对象是否已初始化,然后您可以安全地处置它。

If objExcel isnot nothing then objExcel.dispose()
于 2013-10-17T16:32:05.297 回答