Excel实例不会关闭 [英] Excel Instances don't close
问题描述
我不知道这是否在正确的地方但是..
我正在编写一个VB.Net类库,它使用Excel生成工作簿
如果我创建一个Excel实例然后关闭它我可以让.NET破坏Excel的实例,但是当我尝试添加一个工作簿然后结束然后关闭Excel时,实例将不会关闭
代码包括在下面
有没有其他人经历过这个
出了什么问题
Sub foo_Works(
Dim xl作为Excel.Application,wb作为Excel.Workboo
xl = CreateObject(" Excel.application"
wb = xl.Workbooks.Ad
wb.Close(False
ReleaseCOMObject(wb
xl.Quit(
ReleaseCOMObject(xl
xl = Nothin
end su
sub foo_doesntwork(
Dim xl As Excel.Application,wb As Excel.Workboo
xl = CreateObject(" Excel.application"
wb = xl。 Workbooks.Ad
wb.Close(False
ReleaseCOMObject(wb
xl.Quit(
ReleaseCOMObject(xl)
xl = Nothin
结束su
Public Sub ReleaseCOMObject(ByVal pobj As Object
Dim x As Intege
Tr
x = System.Runtime.InteropServices.Marshal.ReleaseComO bject(pobj
Catc
Finall
pobj = Nothin
结束Tr
结束子
I don''t know if this is in the right place but..
I am writing a VB.Net Class Library which uses Excel to produce workbooks
If I create an instance of Excel and then close it I can get .NET to destroy the instance of Excel, however as soon as I try to add a workbook and then end then close Excel the instance will not close
Code is included below
Has anyone else experienced this
What is going wrong
Sub foo_Works(
Dim xl As Excel.Application, wb As Excel.Workboo
xl = CreateObject("Excel.application"
wb = xl.Workbooks.Ad
wb.Close(False
ReleaseCOMObject(wb
xl.Quit(
ReleaseCOMObject(xl
xl = Nothin
end su
sub foo_doesntwork(
Dim xl As Excel.Application, wb As Excel.Workboo
xl = CreateObject("Excel.application"
wb = xl.Workbooks.Ad
wb.Close(False
ReleaseCOMObject(wb
xl.Quit(
ReleaseCOMObject(xl
xl = Nothin
end su
Public Sub ReleaseCOMObject(ByVal pobj As Object
Dim x As Intege
Tr
x = System.Runtime.InteropServices.Marshal.ReleaseComO bject(pobj
Catc
Finall
pobj = Nothin
End Tr
End Sub
推荐答案
在xl = no之后添加GC.Collect。
Ken
--------------------
" IanW" <一个******* @ discussions.microsoft.com>在消息中写道
新闻:D4 ********************************** @ microsof t.com ...
Hi,
Add GC.Collect after xl=nothing.
Ken
--------------------
"IanW" <an*******@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
我不知道这是否在正确的地方但是...
我正在编写一个VB.Net类库,它使用Excel生成工作簿。
如果我创建一个Excel实例然后关闭它我可以让.NET破坏Excel实例,但是一旦我尝试添加一个工作簿
然后结束然后关闭Excel实例将不会关闭。
代码包括在下面。
有没有其他人经历过这个?
出了什么问题?
Sub foo_Works ()
Dim xl作为Excel.Application,wb作为Excel.Workbook
xl = CreateObject(" Excel.application")
wb = xl.Workbooks.Add
wb.Close(False)
ReleaseCOMObject(wb)
xl.Quit()
ReleaseCOMObject(xl)
xl =没什么
结束子
sub foo_doesntwork()
Dim xl作为Excel.Application,wb作为Excel.Workbook
xl = CreateObject(" Excel.application")
wb = xl.Workbooks.Add
wb.Close(False)
ReleaseCOMObject(wb)
xl。退出()
ReleaseCOMObject(xl)
xl =没什么
结束子公共子ReleaseCOMObject(ByVal pobj As Object)
昏暗x作为整数
尝试
x = System.Runtime.InteropServices.Marshal.ReleaseComO bject(pobj)
抓住
最后
pobj =没什么
结束尝试
End Sub
I don''t know if this is in the right place but...
I am writing a VB.Net Class Library which uses Excel to produce workbooks.
If I create an instance of Excel and then close it I can get .NET to
destroy the instance of Excel, however as soon as I try to add a workbook
and then end then close Excel the instance will not close.
Code is included below.
Has anyone else experienced this?
What is going wrong?
Sub foo_Works()
Dim xl As Excel.Application, wb As Excel.Workbook
xl = CreateObject("Excel.application")
wb = xl.Workbooks.Add
wb.Close(False)
ReleaseCOMObject(wb)
xl.Quit()
ReleaseCOMObject(xl)
xl = Nothing
end sub
sub foo_doesntwork()
Dim xl As Excel.Application, wb As Excel.Workbook
xl = CreateObject("Excel.application")
wb = xl.Workbooks.Add
wb.Close(False)
ReleaseCOMObject(wb)
xl.Quit()
ReleaseCOMObject(xl)
xl = Nothing
end sub
Public Sub ReleaseCOMObject(ByVal pobj As Object)
Dim x As Integer
Try
x = System.Runtime.InteropServices.Marshal.ReleaseComO bject(pobj)
Catch
Finally
pobj = Nothing
End Try
End Sub
在xl = nothing之后添加GC.Collect。 />
肯
--------------------
IanW <一个******* @ discussions.microsoft.com>在消息中写道
新闻:D4 ********************************** @ microsof t.com ...
Hi,
Add GC.Collect after xl=nothing.
Ken
--------------------
"IanW" <an*******@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
我不知道这是否在正确的地方但是...
我正在编写一个VB.Net类库,它使用Excel生成工作簿。
如果我创建一个Excel实例然后关闭它我可以让.NET破坏Excel实例,但是一旦我尝试添加一个工作簿
然后结束然后关闭Excel实例将不会关闭。
代码包括在下面。
有没有其他人经历过这个?
出了什么问题?
Sub foo_Works ()
Dim xl作为Excel.Application,wb作为Excel.Workbook
xl = CreateObject(" Excel.application")
wb = xl.Workbooks.Add
wb.Close(False)
ReleaseCOMObject(wb)
xl.Quit()
ReleaseCOMObject(xl)
xl =没什么
结束子
sub foo_doesntwork()
Dim xl作为Excel.Application,wb作为Excel.Workbook
xl = CreateObject(" Excel.application")
wb = xl.Workbooks.Add
wb.Close(False)
ReleaseCOMObject(wb)
xl。退出()
ReleaseCOMObject(xl)
xl =没什么
结束子公共子ReleaseCOMObject(ByVal pobj As Object)
昏暗x作为整数
尝试
x = System.Runtime.InteropServices.Marshal.ReleaseComO bject(pobj)
抓住
最后
pobj =没什么
结束尝试
End Sub
I don''t know if this is in the right place but...
I am writing a VB.Net Class Library which uses Excel to produce workbooks.
If I create an instance of Excel and then close it I can get .NET to
destroy the instance of Excel, however as soon as I try to add a workbook
and then end then close Excel the instance will not close.
Code is included below.
Has anyone else experienced this?
What is going wrong?
Sub foo_Works()
Dim xl As Excel.Application, wb As Excel.Workbook
xl = CreateObject("Excel.application")
wb = xl.Workbooks.Add
wb.Close(False)
ReleaseCOMObject(wb)
xl.Quit()
ReleaseCOMObject(xl)
xl = Nothing
end sub
sub foo_doesntwork()
Dim xl As Excel.Application, wb As Excel.Workbook
xl = CreateObject("Excel.application")
wb = xl.Workbooks.Add
wb.Close(False)
ReleaseCOMObject(wb)
xl.Quit()
ReleaseCOMObject(xl)
xl = Nothing
end sub
Public Sub ReleaseCOMObject(ByVal pobj As Object)
Dim x As Integer
Try
x = System.Runtime.InteropServices.Marshal.ReleaseComO bject(pobj)
Catch
Finally
pobj = Nothing
End Try
End Sub
嗨
这不能解决问题
有没有人有更多的想法
它是否失败,因为它在类库中
我简单地修改了下面没有工作的代码
公共类TestClas
Public Sub New(
结束Su
公共函数doXLBit(ByVal sPath As String
Dim xlapp As New Excel.Applicatio
Dim xlWB As Excel.Workboo
xlWB = xlApp.Workbooks.Ad
''工作簿操作代码适用于她
xlWB.SaveAs(sPath
xlWB.Close(
RCO(xlWB
xlWB = Nothin
xlApp.Quit(
RCO(xlApp
xlapp = Nothin
GC.Collect(
结束功能
私人功能RCO(ByVal o As Object
Dim i As Intege
Tr >
i = System.Runtime.InteropServices.Marshal.ReleaseComO bject(o
Do while i>
i = System.Runtime.InteropServices.Marshal.ReleaseComO bject ($
Loo
Catc
Finall
o = Nothin
结束Tr
结束功能
结束Clas
这是从Windows表单应用程序中调用的/>
Private Sub CreateXLWorkbook(ByVal sPath As String
Dim T As New TestClass.TestClas
T.doXLBit(sPath)
T = Nothin
结束Su
/ IanW
Hi
This doesn''t fix the problem
Does anyone have any more ideas
Is it failing because it is in a class library
I have simplyfied the code that doesn''t work below
Public Class TestClas
Public Sub New(
End Su
Public Function doXLBit(ByVal sPath As String
Dim xlapp As New Excel.Applicatio
Dim xlWB As Excel.Workboo
xlWB = xlApp.Workbooks.Ad
'' Workbook manipulation code goes her
xlWB.SaveAs(sPath
xlWB.Close(
RCO(xlWB
xlWB = Nothin
xlApp.Quit(
RCO(xlApp
xlapp = Nothin
GC.Collect(
End Functio
Private Function RCO(ByVal o As Object
Dim i As Intege
Tr
i = System.Runtime.InteropServices.Marshal.ReleaseComO bject(o
Do While i >
i = System.Runtime.InteropServices.Marshal.ReleaseComO bject(o
Loo
Catc
Finall
o = Nothin
End Tr
End Functio
End Clas
This is called from a windows form app usin
Private Sub CreateXLWorkbook(ByVal sPath As String
Dim T As New TestClass.TestClas
T.doXLBit(sPath
T = Nothin
End Su
/IanW
这篇关于Excel实例不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!