Excel.exe can't be released from memory

G

Guest

Good day VB.Net seniors and experts,

I have a window service application which will import data from Excel file.
I face a problem

where Excel.exe process cannot be released from memory once the application
finish process an

Excel file. I did put the code to dereference those Excel objects being
created but seem like

the code is not working. Some portion of my code is shown as follows:

------------------------------------------------------------------------------
Imports System.Runtime.InteropServices.Marshal

Public oExcel As Object
Public oBook2 As Object
Public oSheet As Object

Try
oExcel = CreateObject("Excel.Application")
oBook2 = oExcel.Workbooks.Open(Path, 0)
oExcel.GoTo(Reference:="TCell")
oSheet = oBook2.ActiveSheet

// Grab required data from Excel Sheet

Catch err as Exception
// Log to event log and set "MoveToError" flag as TRUE

Finally
ReleaseComObject(oSheet)
oSheet = Nothing
oBook2.Saved = True
oBook2.Close()
ReleaseComObject(oBook2)
oBook2 = Nothing
oExcel.Quit()
ReleaseComObject(oExcel)
oExcel = Nothing
// Move the Excel file to "Error" folder if "MoveToError" flag is TRUE

End Tr
--------------------------------------------------------------------------------

Any idea why I can't terminate the Excel.exe process? The process is still
shown in Task

Manager.

Thanks a lot
 
H

Herfried K. Wagner [MVP]

LBT said:
Good day VB.Net seniors and experts,

I have a window service application which will import data from Excel
file.
I face a problem

where Excel.exe process cannot be released from memory once the
application
finish process an

Excel file. I did put the code to dereference those Excel objects being
created but seem like

the code is not working. Some portion of my code is shown as follows:

------------------------------------------------------------------------------
Imports System.Runtime.InteropServices.Marshal

Public oExcel As Object
Public oBook2 As Object
Public oSheet As Object

Try
oExcel = CreateObject("Excel.Application")
oBook2 = oExcel.Workbooks.Open(Path, 0)
oExcel.GoTo(Reference:="TCell")
oSheet = oBook2.ActiveSheet

// Grab required data from Excel Sheet

Catch err as Exception
// Log to event log and set "MoveToError" flag as TRUE

Finally
ReleaseComObject(oSheet)
oSheet = Nothing
oBook2.Saved = True
oBook2.Close()
ReleaseComObject(oBook2)
oBook2 = Nothing
oExcel.Quit()
ReleaseComObject(oExcel)
oExcel = Nothing
// Move the Excel file to "Error" folder if "MoveToError" flag is TRUE

End Try

PRB: Office Application Does Not Quit After Automation from Visual Studio
..NET Client
<URL:http://support.microsoft.com/?scid=kb;EN-US;317109>
-> "Troubleshooting"
 
U

unglenieks

I have noticed a common thread when people say that microsoft's
"solution" does not work. They try to use an "Open" on the the
workbooks object as opposed to an "Add". My code below does not work,
it leaves Excel open in task manager. I suspect there may be an issue
with Open.

Am I just missing something on the oBooks.Open("c:\Data.xls")
statement?

I am accessing the Microsoft Excel 10.0 Object Library, TypeLib Version
1.4 on a Windows XP Pro Version 2002 Service Pack 1 if that helps.


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

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim oApp As New Excel.Application()
Dim oBooks As Excel.Workbooks = oApp.Workbooks
Dim oBook As Excel.Workbook = oBooks.Open("c:\Data.xls")
' the above line is the only difference
' The original line is below
' 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")
Me.Close()
End Sub
 

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