Wrox Programmer Forums
|
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 8th, 2007, 06:18 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem terminating Excel

I have the following as globals; oSheet is used continuously through the application and I have had the oExcel both as a global and a local (being that it's only used to open the sheet).

Private oExcel As Excel.Application
Private oSheet As Excel.Worksheet

In the function to load the spreadsheet, I have:

Set oExcel = New Excel.Application
Set oSheet = oExcel.Workbooks.Open(MyFile).Sheets.Item(1)

And finally, in the application's file closing function I have the following, which is even repeated (for test) in the form_unload:

If (Not oSheet Is Nothing) Then Set oSheet = Nothing
If (Not oExcel Is Nothing) Then
    oExcel.Workbooks.Close
    Set oExcel = Nothing
End If

When I close the application, the above runs and executes both set=nothing and the workbook close ... but, when I look at the running processes in Task Manager, I see that I still have one process of EXCEL running.

Any ideas to what I have missed?


Regards,

Sean Anderson
__________________
Regards,

Sean Anderson
 
Old May 8th, 2007, 11:53 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Setting a variable to nothing makes it no longer refer to anything. It does nothing to that to which it had been a reference.

In the case of class instantiation, classes are typically destroyed when the reference count becomes zero.

But it is not the same with a reference to a running application. In this case you must explicitly close the application. (BTW: your postings will look better if you use the insert code button [[u]#</u>]):
Code:
    . . .

    oExcel.Workbooks.Close
    oExcel.Quit ' Or .Exit, .Close, etc.  You’ll have to poke about.
    Set oExcel = Nothing
    . . .
 
Old May 9th, 2007, 07:40 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A-ha!

A terrible assumption that terminating the object would do the trick. A lesson learned for me, I see.

Code:
If (Not oSheet Is Nothing) Then Set oSheet = Nothing
If (Not oExcel Is Nothing) Then
    oExcel.Workbooks.Close
    oExcel.Quit
    Set oExcel = Nothing
End If
Note taken about the CODE option, too.

Thanks very much for the speedy response.

Regards,

Sean Anderson





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in opening excel file in MS Excel 2000 kallol Visual C++ 0 November 16th, 2007 05:48 AM
Terminating an outside process. notrosh Visual C++ 0 October 11th, 2006 10:52 AM
Excel Problem Brendan Bartley Access 1 July 7th, 2006 11:16 AM
Terminating External Process [email protected] Pro VB.NET 2002/2003 1 September 25th, 2005 09:28 AM
Excel problem MattF912 Beginning VB 6 2 May 14th, 2005 07:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.