4

The code below works, but the excel.exe process still runs even though I quit Excel. I am using Office 2013 and referencing the correct import for Office.Interop.Excel

Am i missing something

Sub demo()
    Dim xls As New Excel.Application
    Dim book As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    xls.Workbooks.Open("Test.xlsx")
    book = xls.ActiveWorkbook
    oSheet = book.ActiveSheet   

    oSheet.Cells(1, 2).Value = "testing"

    book.Save()
    book.Close()
    xls.Workbooks.Close()
    xls.Quit()

    System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(book)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xls)
    oSheet = Nothing
    book = Nothing
    xls = Nothing
    GC.Collect()
End Sub
Blackwood
  • 4,504
  • 16
  • 32
  • 41
georgieboy
  • 155
  • 1
  • 10

4 Answers4

0

If you are still having issues with it, have you tried to just kill the process?

 Process[] procs = Process.GetProcessesByName("name");
foreach (Process proc in procs)
    proc.Kill();

Not sure if it will work how you want, but it is an idea.

dwb
  • 475
  • 6
  • 31
0

call TryKillProcessByMainWindowHwnd(hWnd); after GC.Collect() and implementation of the method:

[DllImport("user32.dll")]
        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
        public static bool TryKillProcessByMainWindowHwnd(int hWnd)
        {
            uint processID;
            GetWindowThreadProcessId((IntPtr)hWnd, out processID);
            if (processID == 0) return false;
            try
            {
                Process.GetProcessById((int)processID).Kill();
            }
            catch (ArgumentException)
            {
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
0

You should never call Marshal.ReleaseComObject() - the better approach is to call the .NET Garbage Collector to clean up by calling GC.Collect().

You have to be careful to ensure that the code talking to Excel is not in the same method as your GC.Collect(), else the debugger might keep objects alive longer than you'd expect.

The general pattern would be:

Sub WrapperThatCleansUp()

    ' NOTE: Don't call Excel objects in here... 
    '       Debugger would keep alive until end, preventing GC cleanup

    ' Call a separate function that talks to Excel
    DoTheWork()

    ' Now Let the GC clean up (twice, to clean up cycles too)
    GC.Collect()    
    GC.WaitForPendingFinalizers()
    GC.Collect()    
    GC.WaitForPendingFinalizers()

End Sub

Sub DoTheWork()
    Dim xls As New Excel.Application
    Dim book As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    xls.Workbooks.Open("Test.xlsx")
    book = xls.ActiveWorkbook
    oSheet = book.ActiveSheet   

    oSheet.Cells(1, 2).Value = "testing"

    book.Save()
    book.Close()
    xls.Workbooks.Close()
    xls.Quit()

    ' NOTE: No calls the Marshal.ReleaseComObject() are ever needed
End Sub
Govert
  • 16,387
  • 4
  • 60
  • 70
0

I have an app that works on an excel file 2010, the only code I use and closes the excel file is dim workbook As Excel.Workbook => workbook.Close()but i don't close the excel application or all workbooks. try it.

  • This does not provide an answer to the question. You can [search for similar questions](https://stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](https://stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](https://stackoverflow.com/tour) – Yserbius Oct 15 '20 at 16:16