Solved

Closing EXCEL instances with MS Access VBA...

Posted on 2013-11-06
6
459 Views
Last Modified: 2013-11-06
I've tried half a dozen routines I've found on the net for 'killing' EXCEL from within VBA.  Most can kill instances that I started myself MANUALLY by opening a spreadsheet from my Desktop (Win 7 64-bit, but Office of course is 32-bit).

My code within the Access program actually then OPENS 2 spreadsheets and modifies one of them extensively. It seems that it is THIS VERY INSTANCE of Excel that gets 'stuck' in memory, even though I use best-practices for late-binding and Excel object manipulation, etc,  etc.

If I EXIT entirely from the Access program, that instance of Excel WILL get closed.  But I hate to ask my users to exit the entire program every time they process a set of 2 spreadsheets, as they have dozens of sets to process.

Any ideas would be appreciated.
-Bill-
0
Comment
Question by:bcreen
6 Comments
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 100 total points
ID: 39627463
Are you sure that there are no "Exit Routes" from your code that bypass your routines for closing Excel?

For example - Do you have solid error handling in your automation code to close your application object if errors occur?

That's just a recommendation/guess ... If you post your actual code or a sample database, it might clarify why your Excel instance is not closing.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39627500
My best experiences are with early binding.

It sounds like a typical problem with opening and closing of the objects in the correct order, but it is difficult to know without your code.

Here is an example:
Public Sub RenameWorkSheet()

    Dim xls     As Excel.Application
    Dim wkb     As Excel.Workbook
    Dim wks     As Excel.Worksheet
    
    Set xls = New Excel.Application
    Set wkb = xls.Workbooks.Open("c:\test\workbook1.xlsx")
    Set wks = wkb.Worksheets(1)
    
    wks.Name = "My New Name"
    wkb.Close True
    
    Set wks = Nothing
    Set wkb = Nothing
    
    xls.Quit
    
    Set xls = Nothing

End Sub

Open in new window

/gustav
0
 

Author Comment

by:bcreen
ID: 39627572
Here's the code.  Sorry for all of the 'trial-and-error' code that I've left in and commented out.  It was a long, steep learning curve just to get the processing of the two spreadsheets right.  Trying to kill the instance of Excel I created WITHOUT having to exit from Access entirely will just be the 'one last problem' with this entire project....
Thanks in advance.
basExcelWithModuleLevelDims.txt
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 49

Accepted Solution

by:
Gustav Brock earned 250 total points
ID: 39627675
You probably aren't explicit enough. You have to be very strict on this.

Always create/use objects relative to their parents. This is not:

        Sheets("All").Select
        Cells.Select

Note that WorkSheets and Sheets are not the same.

Any and all objects must be closed (if possible) and set to Nothing in reverse order as opened/crated before exiting Excel.
For example, all your ranges are left open.

Finally, using early binding makes this so much easier.
Try my little example and expand step by step on that for some of the things your code does.

Finally, using Select is quite slow. Specify and use ranges if at all possible.

/gustav
0
 
LVL 31

Assisted Solution

by:Helen_Feddema
Helen_Feddema earned 150 total points
ID: 39627710
You might want to try using GetObject, with a fallback in the error handler to CreateObject, so an existing Excel application object will be used, instead of creating a new one.  Sometimes one method works better, sometimes the other.  Here is the sample code I use for this purpose:

   Set appExcel = GetObject(, "Excel.Application")

   'Your code here

ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   'Excel is not running; open Excel with CreateObject
   If Err.Number = 429 Then
      Set appExcel = CreateObject("Excel.Application")
      Resume Next
   Else
      MsgBox "Error No: " & Err.Number _
         & " in " & Me.ActiveControl.Name & " procedure; " _
         & "Description: " & Err.Description
      Resume ErrorHandlerExit
   End If

Open in new window

0
 

Author Closing Comment

by:bcreen
ID: 39627845
Thanks to all.  Since it's definitely in my code, I figured I wasn't doing strict enough 'housekeeping'  with my objects.  Also didn't realize the importance of closing 'ranges' as well as sheets, worksheets, and workbooks.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question