How to end excel.exe process?
i m trying to get excel sheet-name of a excel file from vb.net 3.5 however it opens but excel.exe still remains in the process. How do i stop the pr开发者_StackOverflowocess without killing the excel.exe from task manager?
i realise new excel.application starts new process.
i tried to use quit, close and dispose.......................nothing worked
Below is my code
Dim sheetName As New Excel.XlSheetType
Dim newExcell As New Excel.Application
Dim newWorkBook As Excel.Workbook = app.Workbooks.Open(MyFileName)
Dim worksheetName As String
Dim ws As Excel.Worksheet = CType(WB.Worksheets.Item(1), Excel.Worksheet)
worksheetName = ws.Name
I cannot use kill because there are other excel application running, so how do i close this particular excel.exe from processor. Please help
This KB article describes the problem.
However, it's not an easy one to solve, as you must ensure you call Marshal.ReleaseComObject on every Excel object you instantiate - and it's very easy to miss one.
For example, the following code implicitly references a Workbooks object:
Dim newWorkBook As Excel.Workbook = app.Workbooks.Open(MyFileName)
To release the Workbooks object you need to reference it explicitly, so that you have a reference on which you can call Marshal.ReleaseComObject:
Dim objWorkbooks As Excel.Workbooks = app.Workbooks
Dim newWorkbook As Excel.Workbook = objWorkbooks.Open(MyFileName)
...
Marshal.ReleaseComObject(objWorkbooks)
Marshal.ReleaseComObject(newWorkbook)
...
You also should use Try/Finally to ensure ReleaseComObject is called even if an exception is thrown.
Many people don't recommend killing the process; See How to properly clean up Excel interop objects and Understanding Garbage Collection in .net
Edit: On the other hand many people don't recommend using GC.Collect. See What's so wrong about using GC.Collect()?
In my experience killing the process is the fastest and easiest. This code kills only the exact process that it starts, no other.
Be sure to Close any open workbooks, Quit the application, Release the xlApp object. Finally check to see if the process is still alive and if so then kill it.
Here is the code I use: (works every time)
Sub UsingExcel()
'declare process; will be used later to attach the Excel process
Dim XLProc As Process
'call the sub that will do some work with Excel
'calling Excel in a separate routine will ensure that it is
'out of scope when calling GC.Collect
'this works better especially in debug mode
DoOfficeWork(XLProc)
'Do garbage collection to release the COM pointers
'http://support.microsoft.com/kb/317109
GC.Collect()
GC.WaitForPendingFinalizers()
'I prefer to have two parachutes when dealing with the Excel process
'this is the last answer if garbage collection were to fail
If Not XLProc Is Nothing AndAlso Not XLProc.HasExited Then
XLProc.Kill()
End If
End Sub
'http://msdn.microsoft.com/en-us/library/ms633522%28v=vs.85%29.aspx
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, _
ByRef lpdwProcessId As Integer) As Integer
End Function
Private Sub ExcelWork(ByRef XLProc As Process)
'start the application using late binding
Dim xlApp As Object = CreateObject("Excel.Application")
'or use early binding
'Dim xlApp As Microsoft.Office.Interop.Excel
'get the window handle
Dim xlHWND As Integer = xlApp.hwnd
'this will have the process ID after call to GetWindowThreadProcessId
Dim ProcIdXL As Integer = 0
'get the process ID
GetWindowThreadProcessId(xlHWND, ProcIdXL)
'get the process
XLProc = Process.GetProcessById(ProcIdXL)
'do some work with Excel here using xlApp
'be sure to save and close all workbooks when done
'release all objects used (except xlApp) using NAR(x)
'Quit Excel
xlApp.quit()
'Release
NAR(xlApp)
End Sub
Private Sub NAR(ByVal o As Object)
'http://support.microsoft.com/kb/317109
Try
While (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0)
End While
Catch
Finally
o = Nothing
End Try
End Sub
Use the excel application object. Call Quit()
Like for e.g:
Dim app As New Excel.Application
Try
'...
'After doing your stuff
'...
app.Quit()
Catch ex As Exception
'Log your exception
System.Diagnostics.Print("Error: " + ex.Message)
End Try
精彩评论