Solved

Excel won't quit from VBA in Access

Posted on 2006-07-17
6
351 Views
Last Modified: 2008-02-01
I am trying to refresh Pivot tables from VBA in Access - This code works great except it won't close the Excel App window - it closes the workbook but not the app.  As you can see I am setting the XLS to quit - so why doesn't it?

Here is my current code:

Sub OpenXL_Pivot(pstrWorkbook As String)

    Dim xlWorkbook As Excel.Workbook, xlsApp As Excel.Application
    Dim xlPivotCache As Excel.PivotCache

On Error GoTo Error_Label
    ' get (or open) workbook
    Set xlWorkbook = GetObject(pstrWorkbook)
    ' refresh all pivot tables
    For Each xlPivotCache In xlWorkbook.PivotCaches
        xlPivotCache.Refresh
    Next xlPivotCache
   
Exit_Label:
    On Error Resume Next
    ' make sure everything is visible...
    xlWorkbook.Activate
    xlWorkbook.Windows(1).Visible = True
    xlWorkbook.Application.Visible = True
    xlWorkbook.Save
    xlWorkbook.Close True
    xlsApp.Quit
    Set xlPivotCache = Nothing
    Set xlWorkbook = Nothing
    Set xlsApp = Nothing

   Exit Sub
   
Error_Label:
    MsgBox Err.Description
    Resume Exit_Label

End Sub

Thanks,

Karen
0
Comment
Question by:Karen Schaefer
  • 3
  • 2
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 17125449
Try this right before your exit code:

For Each xlWorkbook IN xlsApp.Workbooks
   xlw.Save
Next

xlsApp.Quit
0
 

Author Comment

by:Karen Schaefer
ID: 17125486
what is the dim xlw for?
K
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 17125487
(Slight correction, had to copy-paste from existing code and I missed one...)

For Each xlWorkbook IN xlsApp.Workbooks
   xlWorkbook.Save
Next

xlsApp.Quit
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

Author Comment

by:Karen Schaefer
ID: 17125491
it closes the wkbooks not the application though.
K
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 17125564
comment out  

On Error Resume Next

then try running it

it may be failing and by putting that statement, u will never know



Try an alternative approach

    Set xlsApp = CreateObject("Excel.Application")
    set xlWorkbook  = xlsApp.Workbooks.Open filename:=pstrWorkbook

then do your stuff, then to close

     xlWorkbook.Save
     xlWorkbook.Close
     xlsApp .Quit
     Set xlWorkbook = Nothing
     Set xlsApp = Nothing



0
 

Author Comment

by:Karen Schaefer
ID: 17125679
thanks that did the job.  I got rid of the

Exit_Label:
    On Error Resume Next
    ' make sure everything is visible...
    xlWorkbook.Activate
    xlWorkbook.Windows(1).Visible = True
    xlWorkbook.Application.Visible = True
    xlWorkbook.Save
    xlWorkbook.Close True
    xlsApp.Quit
    Set xlPivotCache = Nothing
    Set xlWorkbook = Nothing
    Set xlsApp = Nothing

   Exit Sub
portion of my code and it seems to be work great without actually opening the excel application.

Thanks,
Karen
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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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.
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now