Excel stays in Memory

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,294
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have the following snippet of code in Access: -

Code:
            strFilename = "U:\Reports\" & Left(strTrustName, 49)
            DoCmd.TransferSpreadsheet acExport, 8, "qryAllTrustsSummarisedReport", strFilename, True, ""
            
            Set objExcelApp = CreateObject("Excel.Application")
            
            objExcelApp.Visible = True
            Workbooks.Open Filename:="U:\Reports\" & strTrustName & ".xls", ReadOnly:=False
            Worksheets(1).Name = "Cost Savings Report on Food"
            objExcelApp.DisplayAlerts = False
            Worksheets(1).SaveAs strFilename

            objExcelApp.Quit
            Set objExcelApp = Nothing

When I try to open the xls file, I only see the header of the application. I can resolve the problem by ending the application in ther task manager. How do I amend the code so that Excel leaves memory automatically.

Just realised I have placed this in the wrong section. My apologies.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
worksheets(1).SaveAs strFilename
objExcelApp.Quit


Here's your problem.

You are calling the saveas dialog, with excel not shown? Then trying to quit. The quit won't work because the dialog is still showing(on the Visible=false instance of Excel). Change it to this.

worksheets(1).close true, strfilename
objExcelApp.quit

HTH
Cal
 
Upvote 0
I tried that but I get error 438 (Object doesn't support this property or method). The library references I have are: -

Visual Basic for Applications
Microsoft Access 9.0 Object Library
Microsoft Excel 9.0 Object Library
Microsoft DAO 3.6 Object Library
OLE Automation
 
Upvote 0
Give this a try.
Code:
Dim wb as workbook, ws as worksheet
strFilename = "U:\Reports\" & Left(strTrustName, 49) 
DoCmd.TransferSpreadsheet acExport, 8, "qryAllTrustsSummarisedReport", strFilename, True, "" 
            
Set objExcelApp = CreateObject("Excel.Application") 
            
objExcelApp.Visible = True 
set wb=Workbooks.Open Filename:="U:\Reports\" & strTrustName & ".xls", ReadOnly:=False 
set ws = worksheets(1)

ws.Name = "Cost Savings Report on Food" 
objExcelApp.DisplayAlerts = False 
wb.close true, strfilename 

objExcelApp.Quit 
Set objExcelApp = Nothing
 
Upvote 0
I get an error on the following line

Code:
Set wb = Workbooks.Open Filename:="U:\Reports\" & strTrustName & ".xls", ReadOnly:=False

I get a compile error where Filename is highlighted.
 
Upvote 0
Try changing it to this, I fogot to add the brackets, which you need to do when setting it to a variable.

Set wb = Workbooks.Open("U:\Reports\" & strTrustName & ".xls", , False)

Cal
 
Upvote 0
Since I last visited, a colleague of mine has been trying to help me out. Here's the revised code which now works.Thanks for your input.

Code:
            Set objExcelApp = New Excel.Application
            objExcelApp.Visible = False
            
            With objExcelApp
               .Workbooks.Open Filename:=strFilename & ".xls", ReadOnly:=False
               .Workbooks.Item(.Workbooks.Count).Activate
               .ActiveWorkbook.Worksheets(1).Activate
               .ActiveSheet.Name = "Cost Savings Report on Food"
               .DisplayAlerts = False
               .ActiveWorkbook.SaveAs strFilename
               .ActiveWorkbook.Close SaveChanges:=False
               .DisplayAlerts = True
               If .Workbooks.Count = 0 Then
                  .Quit
               End If
            End With
            Set objExcelApp = Nothing

I will try to get my original code and try your last amendment and see if it worked.
 
Upvote 0

Forum statistics

Threads
1,207,402
Messages
6,078,270
Members
446,324
Latest member
JKamlet

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top