simple excel process not killed

Z

zino

the follwoing procedure create a simple Excel object and try to close it, but
the Excel process still in memory


public sub createExcel
Dim excelApp As Excel.Application
excelApp = New Excel.Application
excelApp.Application.Quit() ' I tried excelApp.Quit() and did not work
excelApp = Nothing
End sub

how can I solve it?
 
D

Dave Peterson

Make it visible to see what's causing the trouble:

excelApp.visible = true

before you try to quit

Excel may be waiting for a click of a button from you.
 
W

Wei Lu [MSFT]

Hello Zino,

Could you try running the following VB code and see if you still have Excel
in the
task manger even after you close the VB app?

Private Sub Command1_Click()
Dim XL As New Excel.Application

Dim oBooks As Excel.Workbooks
Set oBooks = XL.Workbooks

Dim oBook As Excel.Workbook
Set oBook = oBooks.Open("C:\temp\Anyold.xls")

oBook.Close False

Set oBook = Nothing
Set oBooks = Nothing
XL.Quit
Set XL = Nothing

End
End Sub

Make sure that you have killed all the instances of Excel from the task
manager
before running the above code.

Also, you may use the following Code to kill the process of Excel in the
task manager directly.

Public Sub EraseExcel()
Dim pTemp As System.Diagnostics.Process()
pTemp = System.Diagnostics.Process.GetProcesses()
Dim pTempProcess As System.Diagnostics.Process

For Each pTempProcess In pTemp
Dim sProcessName As String =
pTempProcess.ProcessName
Dim sProcessID As Int32 =
pTempProcess.Id
If StrComp("excel", sProcessName,
CompareMethod.Text) = 0 Then
Dim pProcessTemp As
System.Diagnostics.Process
pProcessTemp =
Process.GetProcessById(sProcessID)
pProcessTemp.Kill()
pProcessTemp.Close()
End If
Next

End Sub

Also, please make sure all the Anti-Virus software and other third-party
software have been closed.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Z

zino

I tried your: "Private Sub Command1_Click() .... .."
but still, Excel did not get released .... ...

it was released successfully when I called your: "Public Sub
EraseExcel() ... .. "

I'm afraid killing excel process through "System.Diagnostics.Process .. . ."
will lead to potential issue on the server where the application is going to
be hosted and where there is many other applications/processes running ...
....
 
W

Wei Lu [MSFT]

Hello Zino,

You are using the VB.NET, right?

Please refer this KB article to resolve the issue.

317109 Office application does not quit after automation from Visual Studio
..NET client
http://support.microsoft.com/default.aspx?scid=kb;EN-US;317109

When Visual Studio .NET calls a COM object from managed code, it
automatically creates a Runtime Callable Wrapper (RCW). The RCW marshals
calls between the .NET application and the COM object. The RCW keeps a
reference count on the COM object. Therefore, if all references have not
been released on the RCW, the COM object does not quit.

So you need to use the following code:

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oApp As New Excel.Application()
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook As Excel.Workbook = oBooks.Add
Dim oSheet As Excel.Worksheet = oApp.ActiveSheet

NAR(oSheet)
oBook.Close(False)
NAR(oBook)
NAR(oBooks)
oApp.Quit()
NAR(oApp)

Debug.WriteLine("Sleeping...")
System.Threading.Thread.Sleep(5000)
Debug.WriteLine("End Excel")
End Sub


Hope this helps.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Wei Lu [MSFT]

My pleasure, Zino.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
F

fedude

Wei,

What reference library do I have to bind with to have access to:
System.Diagnostics.Process() and Process.GetProcessById()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top