Solved

Closing EXCEL instances with MS Access VBA...

Posted on 2013-11-06
6
447 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now