视觉基础不能关闭excel [英] Visual Basics not closing excel
问题描述
我正在开发一个程序,让您输入有关某人的信息,然后程序将其放入Excel文件中。
打开Excel工作正常,但我不能关闭它。如果我打开任务管理器,仍然有一个Excel的实例打开。我尝试了很多东西,但找不到答案。我使用VB.net,我有Microsoft Office 2013(如果重要)。
Private Sub opslaan_Click(sender As Object, e As EventArgs)处理opslaan.Click
Dim excelApp作为Excel.Application
Dim excelWB作为Excel.Workbook
Dim excelWS As Excel.Worksheet
excelApp = CreateObject( Excel.Application)
excelApp.Visible = False
如果My.Computer.FileSystem.FileExists(My.Settings.Location)然后
excelWB = excelApp.Workbooks.Open (My.Settings.Location)
excelWS = excelWB.Worksheets(1)
Else
excelWB = excelApp.Workbooks.Add
excelWS = excelWB.Worksheets(1)
如果
如果My.Computer.FileSystem.FileExists(My.Settings.Location)然后
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
我希望你能帮助我。
更新的代码 - 仍然不工作
Private Sub opslaan_Click(sender As Object,e As EventArgs)处理opslaan.Click
Dim excelApp As Excel.Application
Dim excelWB作为Excel.Workbook
Dim excelWS As Excel.Worksheet
excelApp = CreateObject(Excel.Application)
excelApp.Visible = False
如果My.Computer.FileSystem.FileExists(My.Settings.Location)然后
excelWB = excelApp.Workbooks.Open(My.Settings.Location)
excelWS = excelWB.Worksheets(1)
Else
excelWB = excelApp.Workbooks.Add
excelWS = excelWB.Worksheets(1)
如果
如果My.Computer .FileSystem.FileExists(My.Settings.Location)然后
excelWB.Save()
Else
excelW B.SaveAs(My.Settings.Location)
End If
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWS)
excelWB.Close(SaveChanges:= False)
系统.Runtime.InteropServices.Marshal.ReleaseComObject(excelWB)
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
Me.Close()
结束Sub
正如@Enigmativity所说,你必须释放com目的。但是,不仅需要释放它,还需要调用GC.Collect()...使用GC.Collect将刷新这些对象并将其从内存中删除,这就是您在任务管理器中看到它们的原因。
Private Sub ReleaseObject(ByVal obj As Object)
尝试
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
Catch ex As Exception
obj = Nothing
最后
GC.Collect()
结束尝试
End Sub
在完成对象后,您需要调用这个...
excelWB.Close()
excelApp.Quit()
ReleaseObject(excelWS)
ReleaseObject(excelWB)
ReleaseObject(excelApp)
我以前有同样的确切问题,您可以在这里阅读更多..
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
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
这篇关于视觉基础不能关闭excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!