How to close EXCEL automation Server in VB?

  • Thread starter Thread starter Wenke Ji
  • Start date Start date
W

Wenke Ji

Hi
I open a Excel workbook using below API:
Set ExcelServer = CreateObject("EXCEL.Application")
Set TargetWorkbook = ExcelServer.Workbooks.Open
(CurrentBook)

Befor the programm exit , I use the below API:
TargetWorkbook.Save
TargetWorkbook.Close

But I check the "Task Management" , the Excel is still at
list.
How can I close the EXCEL automation server completely in
VB program?
Thanks!

BRs
Wenke
 
Hi,

Use this around your .quit code:
Marshal.ReleaseComObject(objws)

objxl.Quit()

Marshal.ReleaseComObject(objxl)

HTH,

Bernie Yaeger
 
I have the same problem.
I have tried lots of things and just couldn't get it to quit.
I think one of my references may not be the correct style and is forcing
Excel to stay open but for the life of me I can't see it.

Anyone want to try?

Public Sub Export2Excel(ByVal dt As DataTable, ByVal strPath As String,
ByRef pb As ProgressBar, ByVal MyOption As String)

'add reference to Microsoft Excel

Dim objXL As Excel.Application

Dim objWBS As Excel.Workbooks

Dim objWB As Excel.Workbook

Dim objWS As Excel.Worksheet

'get a running instance of Excel - this minimizes the number of instances of
Excel in memory!

objXL = CType(GetObject(, "Excel.Application"), Excel.Application)


'create a new instance of Excel if there isn't one running.

'objXL = New Excel.Application

objWBS = objXL.Workbooks

objWB = objWBS.Add

objWS = CType(objWB.Worksheets(1), Excel.Worksheet)

'do stuff here

objWB.Close()

'problem - Excel is staying in Memory even after the Quit. It goes
away when the form is closed.

'Limited to 1 instance stuck in memory instead of unlimited number.

System.Runtime.InteropServices.Marshal.ReleaseComObject(objWS)

System.Runtime.InteropServices.Marshal.ReleaseComObject(objWB)

System.Runtime.InteropServices.Marshal.ReleaseComObject(objWBS)

objXL.Quit()

System.Runtime.InteropServices.Marshal.ReleaseComObject(objXL)

'this does not work to clear out the Excel instance from memory. Even though
it looks like "just the thing".

'http://www.dotnetinterop.com/faq/?q=OfficeCleanup

'http://support.microsoft.com/default.aspx?scid=kb;en-us;317109

'release other objects too! Still does not work. May be the remote call
issue.

objWS = Nothing

objWB = Nothing

objWBS = Nothing

objXL = Nothing

'this does not work to clear out the Excel instance from memory either.

GC.Collect()

GC.WaitForPendingFinalizers()

End Sub
 
Back
Top