473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.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
Jul 21 '05 #1
14 2663
Hi,

Add GC.Collect after xl=nothing.

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

"IanW" <an*******@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.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.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

Jul 21 '05 #2
Hi,

Add GC.Collect after xl=nothing.

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

"IanW" <an*******@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.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.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

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.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
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.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
Jul 21 '05 #5
On Mon, 26 Apr 2004 03:31:03 -0700, "IanW" <bl****@newsgroups.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.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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #6
On Mon, 26 Apr 2004 03:31:03 -0700, "IanW" <bl****@newsgroups.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.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 ~~~ pc******@ameritech.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****@newsgroups.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******@ameritech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #10
On Tue, 27 Apr 2004 01:21:10 -0700, "IanW" <bl****@newsgroups.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******@ameritech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #11
Yes it happens both in the IDE and with the executable.
Jul 21 '05 #12
Yes it happens both in the IDE and with the executable.
Jul 21 '05 #13
On Wed, 28 Apr 2004 02:01:05 -0700, "IanW" <bl****@newsgroups.nospam> wrote:

¤ Yes it happens both in the IDE and with the executable.

I don't see anything in the code you provided that would cause Excel to remain in memory. You may
want to use the Process Explorer utility to verify whether your application is holding a reference
to Excel. (http://www.sysinternals.com/ntw2k/fr.../procexp.shtml)

You can also terminate the process using an API function call, although it wouldn't be the preferred
method.

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal
lpWindowName As String) As Int32
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg
As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

Public Function TerminateExcel()

Dim ClassName As String
Dim WindowHandle As Int32
Dim ReturnVal As Int32
Const WM_QUIT = &H12

Do

ClassName = "XLMain"
WindowHandle = FindWindow(ClassName, Nothing)

If WindowHandle Then
ReturnVal = PostMessage(WindowHandle, WM_QUIT, 0, 0)
End If

Loop Until WindowHandle = 0

End Function
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #14
On Wed, 28 Apr 2004 02:01:05 -0700, "IanW" <bl****@newsgroups.nospam> wrote:

¤ Yes it happens both in the IDE and with the executable.

I don't see anything in the code you provided that would cause Excel to remain in memory. You may
want to use the Process Explorer utility to verify whether your application is holding a reference
to Excel. (http://www.sysinternals.com/ntw2k/fr.../procexp.shtml)

You can also terminate the process using an API function call, although it wouldn't be the preferred
method.

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal
lpWindowName As String) As Int32
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Int32, ByVal wMsg
As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

Public Function TerminateExcel()

Dim ClassName As String
Dim WindowHandle As Int32
Dim ReturnVal As Int32
Const WM_QUIT = &H12

Do

ClassName = "XLMain"
WindowHandle = FindWindow(ClassName, Nothing)

If WindowHandle Then
ReturnVal = PostMessage(WindowHandle, WM_QUIT, 0, 0)
End If

Loop Until WindowHandle = 0

End Function
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Jul 21 '05 #15

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

Similar topics

2
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...
15
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...
9
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.