Solved

Closing EXCEL instances with MS Access VBA...

Posted on 2013-11-06
6
470 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

821 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