[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

Excel won't quit from VBA in Access

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
Karen Schaefer
Asked:
Karen Schaefer
  • 3
  • 2
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Try this right before your exit code:

For Each xlWorkbook IN xlsApp.Workbooks
   xlw.Save
Next

xlsApp.Quit
0
 
Karen SchaeferAuthor Commented:
what is the dim xlw for?
K
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
(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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
Karen SchaeferAuthor Commented:
it closes the wkbooks not the application though.
K
0
 
rockiroadsCommented:
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
 
Karen SchaeferAuthor Commented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now