2

I may be the only person still automating excel this way and I am looking at doing it in ways that better handle COM objects and such, but for now I just import Microsoft.Office.Interop.Excel and go for it. Something that gave me a lot of grief at first was hanging excel.exe processes, but there are tons of resources online for fixing that. At this point I am pretty confident in my abilities to handle it.

One thing I did notice and was wondering about is how does closing a windows forms application interact with excel.exe running in the background and likely more specifically the garbage collector. I have written quite a few excel automation projects and noticed that after I run my program .exe and it has completed there is an excel.exe process that hangs on until I close the pop up window from the windows forms application. Why is that happening?

Does vb.net not call garbage collect until after the window is closed? Looking at the code I write there is no reference to an "on close of application window" event and the garbage collector and release COM objects calls are all right in the code, so it seems curious to me that after the code has executed (which I take to mean garbage collect has been called, as in the code) there is an excel.exe process that stays up until I actually close the windows form application window.

Nothing urgent, more curiosity than anything, but any direct would be appreciated. Thanks.

Community
  • 1
  • 1
asjohnson
  • 1,057
  • 4
  • 17
  • 25
  • Are you certain your code is able to kill Excel? It's been a standing problem for me, such that I abandoned Window-less Excel. After that I would suggest implementing a Form close event that explicitly releases Excel, and don't rely on any garbage collector. – Alan Baljeu Mar 02 '12 at 17:54
  • I keep task manager open and have processes showing with no other instances of excel.exe before I open my program. Then I open my program and execute it (keeping excel invisibile, though I have done it with excel being visible and it doesn't matter) and an instance of excel.exe (or more depending on the code) will appear. There seems to be 1 instance of excel.exe that hangs until I actually close my windows form application, but then it goes away. Killing off excel properly is a hassle, but if you are careful you can definitely do it. See the tutorial I link and the code I link after it. – asjohnson Mar 02 '12 at 18:00

1 Answers1

0

Make sure you are calling the Quit method on the excel object at the end of your code block and you can garbage collect for good measure but it should close in a short while. I had the same problem but found I had to dispose manually.

LongArm
  • 208
  • 1
  • 6
  • I call the quit method on both workbooks and the application. I get rid of all of the instances of excel.exe, but there is one that hangs on until I close the windows forms application that I run it all from and I don't understand why it waits until then. – asjohnson Mar 06 '12 at 14:53