Releasing Excel from memory

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.
 
P

Paul Clement

¤ 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)
 
G

Guest

Thanks Paul,
I tried that but it's still not working. Excel stays in Memory. Any other
suggestion?
Michael.
 
G

Guest

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)
 
P

Paul Clement

¤ 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)
 
G

Guest

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.
 
P

Paul Clement

¤ 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)
 
J

Jim Hughes

Have you tried using the Visual Studio Tools For Office? May fix some of
your issues.
 

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