Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

vb.net - Visual Basics not closing excel

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...