VB.net 以 COM 開啟 EXCEL App. 時不能關掉程序
原來透過vb.net以com object 開 office app. 時. 最後即使用了Application.Quit() 的 method. 只要有一個application的object還未被釋放(e.g. 在vb.net 中 set 成 Nothing), 仍不會自己關掉, 在"工作管理員"的"處理程序"裏仍會見到它.
http://www.blueshop.com.tw/board/show.asp?subcde=BRD200409171252054T6
' 釋放 Excel App
NAR(xlSheet)
'true 將做存檔動作
xlBook.Close(True)
NAR(xlBook)
xlApp.Quit()
NAR(xlApp)
Call KillProcess("Excel.Application")
'以程序名稱,查找還剩下哪些資源尚未釋放
Private Sub KillProcess(ByVal ProcName As String)
Dim thisProc As System.Diagnostics.Process
Dim allRelationalProcs() As Process = System.Diagnostics.Process.GetProcessesByName(ProcName)
For Each thisProc In allRelationalProcs
Try
If Not thisProc.CloseMainWindow() Then
thisProc.Kill()
End If
Catch ex As Exception
MsgBox(ex.GetBaseException.ToString)
End Try
Next
End Sub
'關閉 Excel ,釋放所有 APP 資源
Public Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch e As Exception
MsgBox("關閉 Excel, 發生例外事件!!")
Finally
o = Nothing
End Try
End Sub
' 釋放 Excel App
NAR(xlSheet)
'true 將做存檔動作
xlBook.Close(True)
NAR(xlBook)
xlApp.Quit()
NAR(xlApp)
Call KillProcess("Excel.Application")
'以程序名稱,查找還剩下哪些資源尚未釋放
Private Sub KillProcess(ByVal ProcName As String)
Dim thisProc As System.Diagnostics.Process
Dim allRelationalProcs() As Process = System.Diagnostics.Process.GetProcessesByName(ProcName)
For Each thisProc In allRelationalProcs
Try
If Not thisProc.CloseMainWindow() Then
thisProc.Kill()
End If
Catch ex As Exception
MsgBox(ex.GetBaseException.ToString)
End Try
Next
End Sub
'關閉 Excel ,釋放所有 APP 資源
Public Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch e As Exception
MsgBox("關閉 Excel, 發生例外事件!!")
Finally
o = Nothing
End Try
End Sub
沒有留言:
張貼留言