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