By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Excel.exe can't be released from memory

P: n/a
LBT
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

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


"LBT" <LB*@discussions.microsoft.com> schrieb im Newsbeitrag
news:A5**********************************@microsof t.com...
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
--------------------------------------------------------------------------------

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


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2

P: n/a
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.ReleaseComO bject(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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.