Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am working on a program that lets you enter information about a person, and the program then puts it in a Excel file. Opening Excel works fine, but I cant close it. If I open the taskmanager, there is still a instance of Excel open. I tried a lot of things, but couldnt find the answer. I am using VB.net and i have Microsoft office 2013 (if it matters).

   Private Sub opslaan_Click(sender As Object, e As EventArgs) Handles opslaan.Click
    Dim excelApp As Excel.Application
    Dim excelWB As Excel.Workbook
    Dim excelWS As Excel.Worksheet

    excelApp = CreateObject("Excel.Application")
    excelApp.Visible = False

    If My.Computer.FileSystem.FileExists(My.Settings.Location) Then
        excelWB = excelApp.Workbooks.Open(My.Settings.Location)
        excelWS = excelWB.Worksheets(1)
    Else
        excelWB = excelApp.Workbooks.Add
        excelWS = excelWB.Worksheets(1)
    End If


    If My.Computer.FileSystem.FileExists(My.Settings.Location) Then
        excelWB.Save()
    Else
        excelWB.SaveAs(My.Settings.Location)
    End If
    excelWS = Nothing
    excelWB.Close(SaveChanges:=False)
    excelWB = Nothing
    excelApp.Quit()
    excelApp = Nothing
    Me.Close()
End Sub

I hope you can help me.


Updated code - still not working

    Private Sub opslaan_Click(sender As Object, e As EventArgs) Handles opslaan.Click
    Dim excelApp As Excel.Application
    Dim excelWB As Excel.Workbook
    Dim excelWS As Excel.Worksheet

    excelApp = CreateObject("Excel.Application")
    excelApp.Visible = False

    If My.Computer.FileSystem.FileExists(My.Settings.Location) Then
        excelWB = excelApp.Workbooks.Open(My.Settings.Location)
        excelWS = excelWB.Worksheets(1)
    Else
        excelWB = excelApp.Workbooks.Add
        excelWS = excelWB.Worksheets(1)
    End If


    If My.Computer.FileSystem.FileExists(My.Settings.Location) Then
        excelWB.Save()
    Else
        excelWB.SaveAs(My.Settings.Location)
    End If
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWS)
    excelWB.Close(SaveChanges:=False)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWB)
    excelApp.Quit()
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
    Me.Close()
End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
134 views
Welcome To Ask or Share your Answers For Others

1 Answer

As @Enigmativity said, you must release the com object. But not only do you need to release it, you need to call GC.Collect()... Using the GC.Collect will flush these objects and remove them from memory, that's why your seeing it in task manager.

 Private Sub ReleaseObject(ByVal obj As Object)
Try
  System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
Catch ex As Exception
    obj = Nothing
Finally
    GC.Collect()
End Try
 End Sub

You need to call this after your done with your objects...

 excelWB.Close()
 excelApp.Quit()


ReleaseObject(excelWS)
ReleaseObject(excelWB)
ReleaseObject(excelApp)

I had the same exact issue before, you can read more here..

Excel application not quitting after calling quit


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...