Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Closing EXCEL instances with MS Access VBA...

Posted on 2013-11-06
6
Medium Priority
?
580 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 400 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 52

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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 52

Accepted Solution

by:
Gustav Brock earned 1000 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 600 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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