2

My application is a plugin (DLL) of another application. From my application I launch Excel, then simply open a workbook and save it.

Dim oExcel As Object = CreateObject("Excel.Application")
oExcel.Visible = False
oExcel.DisplayAlerts = False
Dim oWorkBook As Object = oExcel.Workbooks.Open(excelFileName)
oWorkBook.Save()
oWorkBook.Close(True)
oExcel.Quit()
oWorkBook = Nothing
oExcel = Nothing

The problem is that the instance of Excel I created does not close until the main application (again my app is a plugin of another application) closes.

Is there anything I can do besides killing the process? I tried System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) and calling the garbage collector to no avail.

TIA.

Atchoum
  • 703
  • 8
  • 16
  • I had a similar problem and found that excel launched a separate session. Instead of just quitting your created session, try finding all sessions and closing. See this [link](http://stackoverflow.com/questions/3526228/vba-script-to-close-every-instance-of-excel-except-itself) for a little guidance. – Automate This Oct 03 '13 at 23:05
  • Thanks, Portland. In this particular case though, the Excel instance does get released when the main application closes. I looked at the proposed code but what if Excel is already open though? I don't want to close all instances. Here is what I do right now, I loop through all Excel processes before I launch Excel, then loop through all processes after the launch to find the one I created and then I kill it at the end of my procedure. But it does not make me feel good. Thought there would be another way. – Atchoum Oct 03 '13 at 23:58

1 Answers1

0

This is a common problem that Excel.exe keeps running, i suggest you detach and release all variables/objects that are connected to it, then set them as nothing and in the end use gc.collect. Becareful, u have to detach ALL variables/object, ranges, spreadsheets, workbooks, cells that you use in your whole code. Then excel.exe will close :)

Afnan Makhdoom
  • 654
  • 1
  • 8
  • 20