Releasing Excel from memory

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have following code in an aspx page:

Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
Dim oExcel As New Excel.Application

Dim oWorkbook As Excel.Workbook
Dim oWorkbooks As Excel.Workbooks
oWorkbooks = oExcel.Workbooks
oWorkbook = oWorkbooks.Open("test.xls")

oExcel.Visible = True

Dim oSheet As Excel.Worksheet = oWorkbook.Worksheets.Item(1)

oWorkbook.Close()

oExcel.Quit()

ReleaseComObject(oSheet)
ReleaseComObject(oWorkbooks)
ReleaseComObject(oWorkbook)
ReleaseComObject(oExcel)

oSheet = Nothing
oWorkbooks = Nothing
oWorkbook = Nothing
oExcel = Nothing

System.GC.Collect()
End Sub

If I remove the oSheet piece, Excel is removed from memory. But when it's
there Excel stays in memory. Can anyone tell me what's wrong?

Thanks, Micke.
 
¤ Hi,
¤
¤ I have following code in an aspx page:
¤
¤ Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
¤ Dim oExcel As New Excel.Application
¤
¤ Dim oWorkbook As Excel.Workbook
¤ Dim oWorkbooks As Excel.Workbooks
¤ oWorkbooks = oExcel.Workbooks
¤ oWorkbook = oWorkbooks.Open("test.xls")
¤
¤ oExcel.Visible = True
¤
¤ Dim oSheet As Excel.Worksheet = oWorkbook.Worksheets.Item(1)
¤
¤ oWorkbook.Close()
¤
¤ oExcel.Quit()
¤
¤ ReleaseComObject(oSheet)
¤ ReleaseComObject(oWorkbooks)
¤ ReleaseComObject(oWorkbook)
¤ ReleaseComObject(oExcel)
¤
¤ oSheet = Nothing
¤ oWorkbooks = Nothing
¤ oWorkbook = Nothing
¤ oExcel = Nothing
¤
¤ System.GC.Collect()
¤ End Sub
¤
¤ If I remove the oSheet piece, Excel is removed from memory. But when it's
¤ there Excel stays in memory. Can anyone tell me what's wrong?


You should be releasing the other COM objects before you call the Quit method for the Excel
Application object.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Thanks Paul,
I tried that but it's still not working. Excel stays in Memory. Any other
suggestion?
Michael.
 
Micke said:
Hi,

I have following code in an aspx page:

Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
Dim oExcel As New Excel.Application

Dim oWorkbook As Excel.Workbook
Dim oWorkbooks As Excel.Workbooks
oWorkbooks = oExcel.Workbooks
oWorkbook = oWorkbooks.Open("test.xls")

oExcel.Visible = True

Dim oSheet As Excel.Worksheet = oWorkbook.Worksheets.Item(1)

oWorkbook.Close()

oExcel.Quit()

ReleaseComObject(oSheet)
ReleaseComObject(oWorkbooks)
ReleaseComObject(oWorkbook)
ReleaseComObject(oExcel)

oSheet = Nothing
oWorkbooks = Nothing
oWorkbook = Nothing
oExcel = Nothing

System.GC.Collect()
End Sub

If I remove the oSheet piece, Excel is removed from memory. But when it's
there Excel stays in memory. Can anyone tell me what's wrong?

Thanks, Micke.


try this just change the order in which excel child are released

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

thanks

sreejith
(e-mail address removed)
 
¤ Thanks Paul,
¤ I tried that but it's still not working. Excel stays in Memory. Any other
¤ suggestion?
¤ Michael.
¤

Could you post the code that you now have?


Paul
~~~~
Microsoft MVP (Visual Basic)
 
I think I have tried to rearrange the lines in every combination possible but
Excel remains in memory. Here is the code I have today:

Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
Dim oExcel As New Excel.Application

Dim oWorkbooks As Excel.Workbooks
Dim oWorkbook As Excel.Workbook
oWorkbooks = oExcel.Workbooks
oWorkbook = oWorkbooks.Open("test.xls")

oExcel.Visible = True

Dim oSheet As Excel.Worksheet = oWorkbook.Worksheets.Item(1)

oWorkbooks.Close()

ReleaseComObject(oSheet)
ReleaseComObject(oWorkbook)
ReleaseComObject(oWorkbooks)

oExcel.Quit()
ReleaseComObject(oExcel)

oSheet = Nothing
oWorkbooks = Nothing
oWorkbook = Nothing
oExcel = Nothing

System.GC.Collect()
End Sub

Thanks, Michael.
 
¤ I think I have tried to rearrange the lines in every combination possible but
¤ Excel remains in memory. Here is the code I have today:
¤
¤ Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
¤ Dim oExcel As New Excel.Application
¤
¤ Dim oWorkbooks As Excel.Workbooks
¤ Dim oWorkbook As Excel.Workbook
¤ oWorkbooks = oExcel.Workbooks
¤ oWorkbook = oWorkbooks.Open("test.xls")
¤
¤ oExcel.Visible = True
¤
¤ Dim oSheet As Excel.Worksheet = oWorkbook.Worksheets.Item(1)
¤
¤ oWorkbooks.Close()
¤
¤ ReleaseComObject(oSheet)
¤ ReleaseComObject(oWorkbook)
¤ ReleaseComObject(oWorkbooks)
¤
¤ oExcel.Quit()
¤ ReleaseComObject(oExcel)
¤
¤ oSheet = Nothing
¤ oWorkbooks = Nothing
¤ oWorkbook = Nothing
¤ oExcel = Nothing
¤
¤ System.GC.Collect()
¤ End Sub

I'm thinking the problem has something to do with running Excel under an ASP.NET application. I
don't seem to have any issue when running it under a VB.NET app.

There are issues when using server side automation with the Office applications, which is why
Microsoft doesn't recommend doing this:

http://support.microsoft.com/kb/257757/en/


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Have you tried using the Visual Studio Tools For Office? May fix some of
your issues.
 
Back
Top