1

I have a problem, where a network located shared Excel 2010 file is opened and being edited from VB.net successfully, but when I close it with Excel.Application.Quit(), the window itself closes, but an EXCEL.EXE process remains open.

As a workaround I kill the process if the Process.StartTime matches the time when I opened the excel, but this can a) not kill any excel process, which erroneously previously remained open b) could kill completely different workbooks...

 Dim xlp() As Process = Process.GetProcessesByName("EXCEL")
        For Each Process As Process In xlp
            If Process.StartTime >= datestart And Process.StartTime <= dateEnd Then
                Process.Kill()
            End If
        Next

I tried setting the sheet to saved, but it did not help. However if I unshare the excel table, then the process also dies properly when the workbook, and then the window is closed.

Dim xlApp As Excel.Application = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Dim xlWorkSheet As Excel.Worksheet = Nothing

xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open(Excel_path)
xlWorkSheet = xlWorkBook.Worksheets(sheetName)

' whitchcraft here

xlWorkBook.Close(SaveChanges:=True)
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)

Please ease my misery...

  • This is clearly not VBA but VB.Net – Rory Jan 19 '17 at 12:16
  • As far as I know there is no way around this. :/ – Tom K. Jan 19 '17 at 12:18
  • my bad, corrected – Zsombor Kiss Jan 19 '17 at 12:21
  • I do not understand the reason, maybe someone more experienced could figure it out, but if I add `xlApp.UserControl = True` before `xlApp.Quit()`, then the process disappears. – Zsombor Kiss Jan 19 '17 at 13:02
  • I also realised that in this case the excel.exe process only disappears when I close my Form. Unit then it is still there. – Zsombor Kiss Jan 19 '17 at 13:04
  • This has been asked many times before. I assume that`releaseObject`involves calling `Marshal.ReleaseComObject`. Such attempts to achieve deterministic release of COM objects often fail for various reasons. It is best to allow the GC to do what it was designed to do. If you need deterministic release of the COM objects so that Excel closes completely while your application is running, I suggest you follow the guidance I posted here: [http://stackoverflow.com/a/36578663/2592875](http://stackoverflow.com/a/36578663/2592875) – TnTinMn Jan 19 '17 at 13:22
  • @TnTinMn thank for the info. I also assumed, that this is a frequently occuring problem, however I could not find any question where a shared workbook was involved. I tried your solution, and did not succeed, the garbace collector also cannot remove the excel application. I guess I will have to play around further with the code, so just as you mentioned all unused stuff gets out of scope to become garbage. – Zsombor Kiss Jan 19 '17 at 14:31
  • "unused stuff gets out of scope to become garbage" - that is the key. Are you by chance using a secondary thread to run Excel? If so, make sure that the secondary thread is set for Single-threaded apartment. It is not mandatory, but I seen issues when using thread-pool threads like the BackgroundWorker use that are Multithreaded apartment. – TnTinMn Jan 19 '17 at 15:06

0 Answers0