0

I've created a function in my Vb.Net application where I allow the user to export DataGridView data to Excel. What happens is I save the file, then ask the user if they want to view it. If they chose yes then I open excel and display the data to them like this...

        If x = vbYes Then
            Process.Start("Excel.exe", filename)
        End If

        xlWorkBook.Close()
        xlApp.Quit()
        releaseobject(xlApp)
        releaseobject(xlWorkBook)
        releaseobject(xlWorkSheet)

and

    Private Sub releaseobject(ByVal obj As Object)
    Try
        system.runtime.interopservices.marshal.releasecomobject(obj)
        obj = Nothing
    Catch ex As exception
        obj = Nothing
    Finally
        gc.collect()
    End Try
End Sub

This looks like it should take care of instance of Excel running in the background. I've tested it multiple times. The data opens fine, everything is okay, then I close it. Each time after I run this function I still see Excel runnign in the processes. Any Ideas?

If need be, i can display my other code...lmk

BobSki
  • 1,531
  • 2
  • 25
  • 61
  • 3
    See this explanation: http://stackoverflow.com/questions/14222501/excel-process-not-closing-in-vb-net?rq=1 – David Zemens Nov 01 '16 at 15:13
  • 1
    I've always released the objects in the opposite order you are, i.e. `releaseobject` sheet, then book, then app. But linked question looks like an exact duplicate, even with some identical code. – djv Nov 01 '16 at 15:21
  • Yeah looks like we got our code from the same source :) – BobSki Nov 01 '16 at 15:22
  • 1
    Interestingly I had a Try Catch Finally End Try and that seemed to be the reason for the instance of it running in the background – BobSki Nov 01 '16 at 15:34
  • 1
    In your case @Bobski I think the order you were releasing things may have been a factor. Worksheet, then Workbook, then the Excel App – Jimmy Smith Nov 01 '16 at 18:21
  • 1
    @JimmySmith the thing is jimmy - I had a try catch Finally (here i included releasing) and end try. once I removed it, everything works fine! – BobSki Nov 01 '16 at 18:22
  • Ill try it in the correct order – BobSki Nov 01 '16 at 18:23
  • 1
    Yeah, actually... your .Quit should do the job, but with the code hitting the next line before that finishes then the garbage collector `gc.Collect()` may have been taking Excel for a spin before closing. – Jimmy Smith Nov 01 '16 at 18:25
  • If you're debugging, try stop the program instead of closing Excel. – James Heffer Nov 03 '16 at 10:46

0 Answers0