0

I have a very small and simple VB application developed on Visual Studio whose role is to:

  1. Open an Excel Workbook (which includes some calculations formulae),
  2. Feed into the INPUT cells the values of the parameters,
  3. Retrieve from a pre-defined cell the value of the result.

Excel is run without human interface (i.e. Excel_Application.Visible = False).

Upon completion of the calculation (and retrieval of the result) the following commands are executed:

:
Excel_Workbook.Close(False)
Excel_Workbook = Nothing
Excel_Application.Quit()
Excel_Application = Nothing
:

Even after these statements are executed, when looking at the Task Manager an Excel process is shown. This process is removed when the application is asked to end (e.g. click at the "X" button of the window).

It should be noted that, if I turn on the graphical interface, the icon from the taskbar does disappear after Excel_Application.Quit() but the background process still remains.

I need the ability to leave no background process without closing the application.

I search the web and found a number of suggestions, none of them work.

FDavidov
  • 3,505
  • 6
  • 23
  • 59
  • 1
    Maybe this post can help https://stackoverflow.com/q/158706/1050927, the top 3 (by votes) should help – Prisoner Oct 19 '17 at 09:55
  • 1
    Excel cannot quit until all the interface references are released. That happens when the garbage collector runs, like it will when your vb.net app continues to do useful work. Or when you close it. So a rough diagnostic is that your vb.net app doesn't do anything useful anymore. Yes, you might as well quit it. Or call GC.Collect if want to force it. Be careful where you put it however, it should go in the code that calls this method or it won't do the job when you debug. – Hans Passant Oct 19 '17 at 10:46
  • Thank you @HansPassant for your comment. I should add some more details... The posted code is part of a class that will eventually be part of a DLL. The DLL will be assembled into a SQL SERVER function to be invoked by a stored procedure and hence the GC cannot be invoked from the calling procedure. Meaning, the whole thing must be self-contained within the DLL. One additional observation: when running under debug and manually looping, Excel instances appear but, at certain point, they disappeared (say, when reaching 7 and triggering the 8th, only one was left). – FDavidov Oct 22 '17 at 04:26

1 Answers1

0

I would suggest to get the process of created Excel application and then close it:

Imports System.Diagnostics
Imports Excel = Microsoft.Office.Interop.Excel

Sub UseExcel()

    Dim xlApp = New Excel.Application With {.Visible = True}
    Dim xlHwnd = New IntPtr(xlApp.Hwnd)
    Dim proc = Process.GetProcesses().First(Function(p) p.MainWindowHandle = xlHwnd)

    '// Do the work with Excel...

    '// In the end:
    proc.Kill()

End Sub
JohnyL
  • 6,894
  • 3
  • 22
  • 41