使用Interop后释放Excel
我读过很多帖子来寻找答案,但都与此类似: 在 vb.net 中读取 excel 文件会使 excel 进程挂起< /a>
我的问题是我不退出应用程序...
想法是这样的:
如果用户打开了 Excel,如果他打开了我感兴趣的文件...获取该 Excel 实例并执行任何操作我想做...
但完成后我不会关闭他的文件...我希望他继续工作,问题是当他关闭 Excel 时...该进程仍在运行..并运行...并在用户使用 X 按钮关闭 Excel 后运行...
这就是我尝试执行的操作
这一部分用于了解他是否打开了 Excel,并在 For I 检查文件名我感兴趣。
Try
oApp = GetObject(, "Excel.Application")
libroAbierto = True
For Each libro As Microsoft.Office.Interop.Excel.Workbook In oApp.Workbooks
If libro.Name = EquipoASeccionIdSeccion.Text & ".xlsm" Then
Exit Try
End If
Next
libroAbierto = False
Catch ex As Exception
oApp = New Microsoft.Office.Interop.Excel.Application
End Try
这是我的代码...如果他没有打开 Excel,我会创建一个新实例,打开文件和其他所有内容。
我的代码以此结尾:
If Not libroAbierto Then
libroSeccion.Close(SaveChanges:=True)
oApp.Quit()
Else
oApp.UserControl = True
libroSeccion.Save()
End If
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(libroOriginal)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(libroSeccion)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(origen)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(copiada)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp)
libroOriginal = Nothing
libroSeccion = Nothing
oApp = Nothing
origen = Nothing
copiada = Nothing
nuevosGuardados = True
所以你可以看到,如果我打开文件,我会调用 oApp.Quit() 以及其他所有内容,并且 Excel 进程会在几秒钟(大约 5 秒)后结束。
但是如果我的意思是用户保持文件打开(不调用 Quit()),Excel 进程在用户使用 X 按钮关闭 Excel 后继续运行。
有什么办法可以做我想做的事吗?控制打开的 Excel 实例并释放所有内容,以便当用户使用 X 按钮关闭它时,Excel 进程会正常终止???
谢谢!!!
I've read many post looking for my answer, but all are similar to this:
Reading excel files in vb.net leaves excel process hanging
My problem is that I don't quit the app...
The idea is this:
If a User has Excel Open, if he has the file I'm interested in open... get that Excel instance and do whatever I want to do...
But I don't to close his File after I'm done... I want him to keep working on it, the problem is that when he closes Excel... The process keeps running... and running... and running after the user closes Excel with the X button...
this is how I try to do it
This piece is used to know if he has Excel open, and in the For I check for the file name I'm interested in.
Try
oApp = GetObject(, "Excel.Application")
libroAbierto = True
For Each libro As Microsoft.Office.Interop.Excel.Workbook In oApp.Workbooks
If libro.Name = EquipoASeccionIdSeccion.Text & ".xlsm" Then
Exit Try
End If
Next
libroAbierto = False
Catch ex As Exception
oApp = New Microsoft.Office.Interop.Excel.Application
End Try
here would be my code... if he hasn't Excel open, I create a new instance, open the file and everything else.
My code ends with this:
If Not libroAbierto Then
libroSeccion.Close(SaveChanges:=True)
oApp.Quit()
Else
oApp.UserControl = True
libroSeccion.Save()
End If
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(libroOriginal)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(libroSeccion)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(origen)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(copiada)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oApp)
libroOriginal = Nothing
libroSeccion = Nothing
oApp = Nothing
origen = Nothing
copiada = Nothing
nuevosGuardados = True
So you can see that, if I opened the file, I call oApp.Quit() and everything else and the Excel Process ends after a few seconds (maybe 5 aprox.)
BUT if I mean the user to keep the file open (not calling Quit()), Excel process keeps running after the user closes Excel with the X button.
Is there any way to do what I try to do?? Control a open instance of excel and releasing everything so when the user closes it with the X button, the Excel Process dies normally???
Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,最好使用
CreateObject
而不是GetObject(, "Excel.Application")
(请参阅 http://support.microsoft.com/kb/288902/en 和 http://technet.microsoft.com/en-us/library/ee176980.aspx)。然后在调用 FinalReleaseComObject 和 allocateinh 之前调用Quit
方法,什么都不会有清晰的感觉。It seems to me it would be better to use
CreateObject
instead ofGetObject(, "Excel.Application")
(see http://support.microsoft.com/kb/288902/en and http://technet.microsoft.com/en-us/library/ee176980.aspx). Then call ofQuit
method before calling of FinalReleaseComObject and assigninh Nothing will be have a clear sense.