Excel Instances don't close

G

Guest

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.ReleaseComObject(pobj
Catc
Finall
pobj = Nothin
End Tr
End Sub
 
G

Guest

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.ReleaseComObject(o
Do While i >
i = System.Runtime.InteropServices.Marshal.ReleaseComObject(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
 
P

Paul Clement

¤ 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 TestClass
¤
¤ Public Sub New()
¤
¤ End Sub
¤
¤ Public Function doXLBit(ByVal sPath As String)
¤ Dim xlapp As New Excel.Application
¤ Dim xlWB As Excel.Workbook
¤
¤ xlWB = xlApp.Workbooks.Add
¤
¤ ' Workbook manipulation code goes here
¤

This is rather important code. Are you creating any other Excel objects without setting them to an
object reference? If you have any implicit object references this can cause Excel to remain in
memory.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
G

Guest

Sorry I didn't make it clear, that comment is just a placeholder it is there for my memory
The instance remains open with no code there. If you create a project as it is it leaves the instance open
There will be code that generates worksheets but I can't get past the instance staying open
BTW I am using Excel 2000, would this make a difference

/IanW
 
P

Paul Clement

¤ Sorry I didn't make it clear, that comment is just a placeholder it is there for my memory.
¤ The instance remains open with no code there. If you create a project as it is it leaves the instance open.
¤ There will be code that generates worksheets but I can't get past the instance staying open.
¤ BTW I am using Excel 2000, would this make a difference?

Version shouldn't make any difference. Does this happen when running from both the IDE and from the
resulting executable?


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top