Solved

Closing EXCEL instances with MS Access VBA...

Posted on 2013-11-06
6
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 50

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 50

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Out in left field or not to far off base 8 67
Access Query function 4 50
Importing specific cell from Excel to MS Access 17 50
SUBFORM on ACCESS 2013 8 32
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

737 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