473,803 Members | 3,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excel Instances don't close

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.Applicati on, wb As Excel.Workboo

xl = CreateObject("E xcel.applicatio n"
wb = xl.Workbooks.Ad
wb.Close(False
ReleaseCOMObjec t(wb
xl.Quit(
ReleaseCOMObjec t(xl
xl = Nothin

end su

sub foo_doesntwork(
Dim xl As Excel.Applicati on, wb As Excel.Workboo

xl = CreateObject("E xcel.applicatio n"
wb = xl.Workbooks.Ad
wb.Close(False
ReleaseCOMObjec t(wb
xl.Quit(
ReleaseCOMObjec t(xl
xl = Nothin

end su

Public Sub ReleaseCOMObjec t(ByVal pobj As Object
Dim x As Intege
Tr
x = System.Runtime. InteropServices .Marshal.Releas eComObject(pobj
Catc
Finall
pobj = Nothin
End Tr
End Sub
Jul 21 '05 #1
14 2712
Hi,

Add GC.Collect after xl=nothing.

Ken
--------------------

"IanW" <an*******@disc ussions.microso ft.com> wrote in message
news:D4******** *************** ***********@mic rosoft.com...
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.Applicati on, wb As Excel.Workbook

xl = CreateObject("E xcel.applicatio n")
wb = xl.Workbooks.Ad d
wb.Close(False)
ReleaseCOMObjec t(wb)
xl.Quit()
ReleaseCOMObjec t(xl)
xl = Nothing

end sub

sub foo_doesntwork( )
Dim xl As Excel.Applicati on, wb As Excel.Workbook

xl = CreateObject("E xcel.applicatio n")
wb = xl.Workbooks.Ad d
wb.Close(False)
ReleaseCOMObjec t(wb)
xl.Quit()
ReleaseCOMObjec t(xl)
xl = Nothing

end sub

Public Sub ReleaseCOMObjec t(ByVal pobj As Object)
Dim x As Integer
Try
x = System.Runtime. InteropServices .Marshal.Releas eComObject(pobj )
Catch
Finally
pobj = Nothing
End Try
End Sub

Jul 21 '05 #2
Hi,

Add GC.Collect after xl=nothing.

Ken
--------------------

"IanW" <an*******@disc ussions.microso ft.com> wrote in message
news:D4******** *************** ***********@mic rosoft.com...
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.Applicati on, wb As Excel.Workbook

xl = CreateObject("E xcel.applicatio n")
wb = xl.Workbooks.Ad d
wb.Close(False)
ReleaseCOMObjec t(wb)
xl.Quit()
ReleaseCOMObjec t(xl)
xl = Nothing

end sub

sub foo_doesntwork( )
Dim xl As Excel.Applicati on, wb As Excel.Workbook

xl = CreateObject("E xcel.applicatio n")
wb = xl.Workbooks.Ad d
wb.Close(False)
ReleaseCOMObjec t(wb)
xl.Quit()
ReleaseCOMObjec t(xl)
xl = Nothing

end sub

Public Sub ReleaseCOMObjec t(ByVal pobj As Object)
Dim x As Integer
Try
x = System.Runtime. InteropServices .Marshal.Releas eComObject(pobj )
Catch
Finally
pobj = Nothing
End Try
End Sub

Jul 21 '05 #3
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.Applicati o
Dim xlWB As Excel.Workboo

xlWB = xlApp.Workbooks .Ad

' Workbook manipulation code goes her

xlWB.SaveAs(sPa th
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.Releas eComObject(o
Do While i >
i = System.Runtime. InteropServices .Marshal.Releas eComObject(o
Loo
Catc
Finall
o = Nothin
End Tr

End Functio

End Clas

This is called from a windows form app usin

Private Sub CreateXLWorkboo k(ByVal sPath As String
Dim T As New TestClass.TestC las

T.doXLBit(sPath

T = Nothin

End Su

/IanW
Jul 21 '05 #4
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.Applicati o
Dim xlWB As Excel.Workboo

xlWB = xlApp.Workbooks .Ad

' Workbook manipulation code goes her

xlWB.SaveAs(sPa th
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.Releas eComObject(o
Do While i >
i = System.Runtime. InteropServices .Marshal.Releas eComObject(o
Loo
Catc
Finall
o = Nothin
End Tr

End Functio

End Clas

This is called from a windows form app usin

Private Sub CreateXLWorkboo k(ByVal sPath As String
Dim T As New TestClass.TestC las

T.doXLBit(sPath

T = Nothin

End Su

/IanW
Jul 21 '05 #5
On Mon, 26 Apr 2004 03:31:03 -0700, "IanW" <bl****@newsgro ups.nospam> wrote:

¤ 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.Applicati on
¤ 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 ~~~ pc******@amerit ech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #6
On Mon, 26 Apr 2004 03:31:03 -0700, "IanW" <bl****@newsgro ups.nospam> wrote:

¤ 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.Applicati on
¤ 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 ~~~ pc******@amerit ech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #7
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
Jul 21 '05 #8
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
Jul 21 '05 #9
On Tue, 27 Apr 2004 01:21:10 -0700, "IanW" <bl****@newsgro ups.nospam> wrote:

¤ 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 ~~~ pc******@amerit ech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2095
by: Lorenzo Melato | last post by:
Hi everyone, I have a very strange problem. If I open a .NET application from EXCEL using SHELL function, when I close EXCEL, the EXCEL.EXE process remain active and I must close it by Task manager. If I open a normal application (I.E. Notepad.exe) in the same manner, I have no problem.
15
813
by: IanW | last post by:
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...
9
3150
by: fahadqureshi | last post by:
I am running two visual basic modules in Access and keep coming across an annoying problem. Both the vb modules transfer an excel spreadsheet to an access database but for some reason after the module has executed the excel spread sheets remain open in memory. So if for some reason i try to go open the spreadsheets in excel i get a message saying that the spreadsheet is locked and can only be open in read-only mode. When i press...
2
3157
by: Bassem | last post by:
I'm working on simple chat program via serial port. It works fine, but i face this problem. When i try to open the exe, UnauthorizedAccess exception occurs, i can't use COM1 becuase someone else has it. I checked it and found that another copy of the program still work (by task manager). That's mean closing the program will not close the serial port. I tried these : Note: I read it using a separate thread. 1)override OnClose to, close the...
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10550
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.