473,386 Members | 1,791 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,386 software developers and data experts.

Excel instance staying open

I have a DLL that opens an Excel workbook and add-in.
If I close the app that calls the DLL before closing Excel, when I close
Excel everything is fine.
But if I close Excel while the app is still open, an instance of Excel
remains in the background. Then even if I close the app, it remains there.
What am I suppose to do, in my DLL, to completely release Excel. If I use
oExcel.Quit() then I shut down Excel...

TIA
Nov 21 '05 #1
20 6474
* "Atchoum" <goglus@DONT_SPAM_ME_videotron.ca> scripsit:
But if I close Excel while the app is still open, an instance of Excel
remains in the background. Then even if I close the app, it remains there.
What am I suppose to do, in my DLL, to completely release Excel. If I use
oExcel.Quit() then I shut down Excel...


PRB: Office Application Does Not Quit After Automation from Visual Studio .NET Client
<URL:http://support.microsoft.com/?scid=kb;EN-US;317109>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2
Herfried,

Thanks. I can't seem to get it right thought. My program's objective is
simply to open a workboook and launch an add-in. I don,t want to close
anything once I am done, but I want obviously the program to release Excel.
Here is excerpt of my code. What am I doing wrong?

Private sub Main()
Dim oExcel As Object
Dim oWorkbookAddin As Object
Dim oWorkbook As Object
Dim oBooks As Object
oExcel = CreateObject("Excel.Application")
oBooks = oExcel.Workbooks
oWorkbookAddin = oExcel.Workbooks
oWorkbook = oExcel.Workbooks
oWorkbookAddin = oBooks.Open(cAppPath &
"\Addin_Excel_Templates.xla", AddToMRU:=False)
oWorkbook = oBooks.Open(Template, Editable:=OpenForEdition)
oWorkbook.Application.Run("Addin_Excel_Templates.x la!AddIn_Start")
NAR(oWorkbook)
NAR(oWorkbookAddin)
NAR(oBooks)
NAR(oExcel)
End Sub
Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
Finally
o = Nothing
End Try
End Sub

TIA,

Atchoum
Nov 21 '05 #3
hi

For me i will call

oWorkBook.Close
oExcel.Quit
NAR(oExcel)

It works fine

Regards,
norton

"Atchoum" <goglus@DONT_SPAM_ME_videotron.ca> wrote in message
news:Zf*********************@wagner.videotron.net. ..
Herfried,

Thanks. I can't seem to get it right thought. My program's objective is
simply to open a workboook and launch an add-in. I don,t want to close
anything once I am done, but I want obviously the program to release Excel. Here is excerpt of my code. What am I doing wrong?

Private sub Main()
Dim oExcel As Object
Dim oWorkbookAddin As Object
Dim oWorkbook As Object
Dim oBooks As Object
oExcel = CreateObject("Excel.Application")
oBooks = oExcel.Workbooks
oWorkbookAddin = oExcel.Workbooks
oWorkbook = oExcel.Workbooks
oWorkbookAddin = oBooks.Open(cAppPath &
"\Addin_Excel_Templates.xla", AddToMRU:=False)
oWorkbook = oBooks.Open(Template, Editable:=OpenForEdition)
oWorkbook.Application.Run("Addin_Excel_Templates.x la!AddIn_Start")
NAR(oWorkbook)
NAR(oWorkbookAddin)
NAR(oBooks)
NAR(oExcel)
End Sub
Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
Finally
o = Nothing
End Try
End Sub

TIA,

Atchoum

Nov 21 '05 #4
On Fri, 13 Aug 2004 23:29:20 -0400, "Atchoum" <goglus@DONT_SPAM_ME_videotron.ca> wrote:

I have a DLL that opens an Excel workbook and add-in.
If I close the app that calls the DLL before closing Excel, when I close
Excel everything is fine.
But if I close Excel while the app is still open, an instance of Excel
remains in the background. Then even if I close the app, it remains there.
What am I suppose to do, in my DLL, to completely release Excel. If I use
oExcel.Quit() then I shut down Excel...

Try setting the UserControl property of the Application object to True.
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #5
But then Excel will close which I don't want!

Atchoum
Nov 21 '05 #6
Paul,

Thanks but it does not seem to make a difference...

Atchoum
Nov 21 '05 #7
On Mon, 16 Aug 2004 15:22:55 -0400, "Atchoum" <goglus@DONT_SPAM_ME_videotron.ca> wrote:

Paul,

Thanks but it does not seem to make a difference...

Atchoum


OK, so if you set the Application UserControl property to True (Visible property must also be True)
and destroy the other objects (that would include the Excel object) it still has no affect?
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #8
To close excel, you need to add the line

Excel.Application.Quit

Mike Ober.

"Paul Clement" <Us***********************@swspectrum.com> wrote in message
news:g1********************************@4ax.com...
On Mon, 16 Aug 2004 15:22:55 -0400, "Atchoum" <goglus@DONT_SPAM_ME_videotron.ca> wrote:
Paul,

Thanks but it does not seem to make a difference...

Atchoum


OK, so if you set the Application UserControl property to True (Visible property must also be True) and destroy the other objects (that would include the Excel object) it still has no affect?

Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)

Nov 21 '05 #9
Atchoum:

The way Interop in .NET works is by creating a proxy between the excel
object and your application. This proxy holds a reference to your COM
object, and the COM object will exist as long as there is a reference to it
(the proxy).

When a variable goes out of scope (a proxy), you cannot see it anymore,
but it still exists until it is Garbage Collected. Try

--> GC.Collect() 'This will make Excel close.

This is not the right thing to do, though. You should manually release
your com objects

Dim oCell As Excel.Cell
Dim oFont As Excel.Font = oCell.Font
oFont.Bold = True
Marshal.ReleaseComObject(oFont) 'This is what you want
Marshal.ReleaseComObject(oCell)

* If I do oCell.Font.Bold = True, a proxy will be created for font, but you
won't be able to release it. You should declare a variable for EVERY COM
OBJECT YOU WANT TO USE, even if you are accesing a subproperty of
something.

Hope this helps, and shows you the light.

Nov 21 '05 #10
Paul,
By application, you mean the Excel application?
I tried but it does not make a difference.

After many test, it boils down to this code: The s=Range.FindNext(s) is the
line that keeps the instance open.
If you comment it out, everything will close properly.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
oExcel = CreateObject("Excel.Application")
oWorkbook = oExcel.Workbooks.Open(Application.StartupPath &
"\test.xls")
For i = 1 To oWorkbook.Sheets.Count
Range = oWorkbook.Sheets(i).UsedRange
s = Range.Find("[[C:", lookin:=-4163) 'xlValues
If Not s Is Nothing Then
If Not s Is Nothing Then
sFirstAddress = s.Address
Do
s = Range.FindNext(s)
Loop While Not s Is Nothing And s.Address <>
sFirstAddress
End If
End If

Next i
oExcel.Visible = True
NAR(s)
NAR(oWorkbook)
oExcel = Nothing
NAR(oExcel)
GC.Collect()
End Sub

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
Finally
o = Nothing
End Try
End Sub
Nov 21 '05 #11
Thanks for the explanations.
The one line that makes it not release Excel is
s = Range.FindNext(s). Even if I use the GarbageCollector.
What am I doing wrong?

in:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
oExcel = CreateObject("Excel.Application")
oWorkbook = oExcel.Workbooks.Open(Application.StartupPath &
"\test.xls", Editable:=True)
For i = 1 To oWorkbook.Sheets.Count
Range = oWorkbook.Sheets(i).UsedRange
s = Range.Find("[[C:", lookin:=-4163) 'xlValues
If Not s Is Nothing Then
If Not s Is Nothing Then
sFirstAddress = s.Address
Do
s = Range.FindNext(s)
Loop While Not s Is Nothing And s.Address <>
sFirstAddress
End If
End If

Next i
oExcel.Visible = True
NAR(s)
NAR(oWorkbook)
oExcel = Nothing
NAR(oExcel)
GC.Collect()
End Sub

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
Finally
o = Nothing
End Try
End Sub
Nov 21 '05 #12
Atchoum wrote:
Thanks for the explanations.
The one line that makes it not release Excel is
s = Range.FindNext(s). Even if I use the GarbageCollector.
What am I doing wrong?


Here, you're losing a reference to the proxy of the first 's', and you won't
be able to release NAR(s)

Try

newS = Range.FindNext(s)
NAR(s)
s = newS
Nov 21 '05 #13
On Tue, 17 Aug 2004 15:09:33 -0400, "Atchoum" <goglus@DONT_SPAM_ME_videotron.ca> wrote:

Paul,
By application, you mean the Excel application?
I tried but it does not make a difference.

After many test, it boils down to this code: The s=Range.FindNext(s) is the
line that keeps the instance open.
If you comment it out, everything will close properly.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
oExcel = CreateObject("Excel.Application")
oWorkbook = oExcel.Workbooks.Open(Application.StartupPath &
"\test.xls")
For i = 1 To oWorkbook.Sheets.Count
Range = oWorkbook.Sheets(i).UsedRange
s = Range.Find("[[C:", lookin:=-4163) 'xlValues
If Not s Is Nothing Then
If Not s Is Nothing Then
sFirstAddress = s.Address
Do
s = Range.FindNext(s)
Loop While Not s Is Nothing And s.Address <>
sFirstAddress
End If
End If

Next i
oExcel.Visible = True
NAR(s)
NAR(oWorkbook)
oExcel = Nothing
NAR(oExcel)
GC.Collect()
End Sub

Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
Finally
o = Nothing
End Try
End Sub


Make certain to destroy any objects you create. This would include the Range object (probably both
"Range" and "s" in your example). In addition, try not to create any implicit objects. In other
words, always set methods that return objects to variables so that they can be disposed of.
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #14
Paul,

I tried to dispose of Range and s but to no avail even if I do it in the
loop itself so that they are disposed of before they are re-assigned... This
is driving me nuts.

Atchoum
Nov 21 '05 #15
Codo,

Sounded like a good idea but it does not work. SIGH.

Atchoum
Nov 21 '05 #16
Atchoum wrote:
Codo,

Sounded like a good idea but it does not work. SIGH.

Atchoum

I don't have VB at home (I'm on Linux/Java), but I'm going to test the code
at the office. Let's see what's going on...
Nov 21 '05 #17
Thanks. I am at a loss here...

Atchoum
Nov 21 '05 #18
Atchoum wrote:
Thanks. I am at a loss here...

Atchoum


Here we are... I've tested this code. Notice that whenever an object might
be returned, I keep it in a variable so I can dispose of it...

e.g. Workbooks.Sheets(1).Range(1, 1) involves three objects. You should
declare a variable for each...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
* * * * Dim oExcel As Excel.Application = New Excel.Application
* * * * Dim oWorkbooks As Excel.Workbooks = oExcel.Workbooks
* * * * Dim oWorkbook As Excel.Workbook =
oWorkbooks.Open(Application.StartupPath & "\test.xls", Editable:=True)
* * * * Dim oSheets As Excel.Sheets = oWorkbook.Sheets

* * * * Dim oRange As Excel.Range
* * * * Dim oSheet As Excel.Worksheet
* * * * Dim s As Excel.Range
* * * * Dim nextS As Excel.Range

* * * * Dim sFirstAddress As String

* * * * Dim i As Integer
* * * * For i = 1 To oSheets.Count
* * * * * * oSheet = CType(oSheets(i), Excel.Worksheet)
* * * * * * oRange = oSheet.UsedRange

* * * * * * s = oRange.Find("[[C:", lookin:=-4163) 'xlValues

* * * * * * If Not s Is Nothing Then
* * * * * * * * sFirstAddress = s.Address
* * * * * * * * Do
* * * * * * * * * * nextS = oRange.FindNext(s)
* * * * * * * * * * NAR(s)
* * * * * * * * * * s = nextS
* * * * * * * * Loop While Not s Is Nothing And s.Address <> sFirstAddress
* * * * * * End If

* * * * * * NAR(oRange)
* * * * * * NAR(oSheet)

* * * * Next i

* * * * oExcel.Visible = True
* * * * NAR(oSheets)
* * * * NAR(oWorkbook)
* * * * NAR(oWorkbooks)
* * * * NAR(oExcel)
* * End Sub

* * Private Sub NAR(ByVal o As Object)
* * * * Try
* * * * * * System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
* * * * Finally
* * * * * * o = Nothing
* * * * End Try
* * End Sub
Let me know if it worked!
Nov 21 '05 #19
Codo,

No, I am still having the same problem. Have you been able to test it
yourself? Did it work on your system?
On mine, it still is the s = oRange.Find("[[C:", lookin:=-4163) which is the
culprit...

Atchoum
Nov 21 '05 #20
On Wed, 18 Aug 2004 20:02:22 -0400, "Atchoum" <goglus@DONT_SPAM_ME_videotron.ca> wrote:

Paul,

I tried to dispose of Range and s but to no avail even if I do it in the
loop itself so that they are disposed of before they are re-assigned... This
is driving me nuts.

Atchoum


Well if all else fails you can always use API function calls to kill the process:

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)
Nov 21 '05 #21

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

Similar topics

14
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...
8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
5
by: RoboSchro | last post by:
I'm building a reporting system in asp.net which needs an "export to Excel" function. We have Excel 2000 in the office. So I've added a reference to the Microsoft Excel 9.0 Object Library. Having...
5
by: Wenke Ji | last post by:
Hi I open a Excel workbook using below API: Set ExcelServer = CreateObject("EXCEL.Application") Set TargetWorkbook = ExcelServer.Workbooks.Open (CurrentBook) Befor the programm exit , I use...
2
by: Atley | last post by:
I have written an application that exports data from SQL to Excel. It all works perfectly except that if you open the Task Manager after running my application, there is an instance of Excel in...
0
by: totallyfake | last post by:
Trying to post this response, but it never shows up in the thread.... so testing a new message You are not fully qualifying Range in several places. This will cause an orphaned instance of...
4
by: Leeor Chernov | last post by:
Hi , I Used the object Excell Application(COM INTEROP) and it is staying as an open process, And the owner(User Name) is ASPNET Altough I try to release it with :...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.