
Access/Excel Automation - Excel Not Closing
Here is my version of your code.
It opens and closes a new application.
The major change to the code involves setting references to all objects used:
The application
The workbook
The worksheet
Then using the references.
All range callouts are preceded by the appropriate object. If you don't, then
the objects may not be released, causing the application to remain open.
'****Revised Code Starts Here****
Sub GoogleWontClosePost()
Dim appEx As Excel.Application
Dim wbkNew1 As Excel.Workbook
Dim wksNew1 As Excel.Worksheet
'Open the Excel Application
Set appEx = CreateObject(Class:="Excel.Application")
Set wbkNew1 = appEx.Workbooks.Open(Filename:="c:\Export.xls")
Set wksNew1 = wbkNew1.Worksheets("Export1")
'Default status is not visible
' appEx.Visible = True
'Copy Range, select destination range then paste
'Avoid using "ActiveSheet"
'Precede all range callouts with the appropriate object - prevents "orphans"
With wksNew1.Range("R2")
.FormulaR1C1 = "=RC[-14]&RC[-2]&RC[-13]"
.Copy
End With
wksNew1.Range("R2:R150").Select
wksNew1.Paste
appEx.CutCopyMode = False
'Delete Some Unneeded Columns
wksNew1.Columns("S").Delete Shift:=xlToLeft
wksNew1.Columns("O:P").Delete Shift:=xlToLeft
appEx.DisplayAlerts = False
'If changing file format then use:
wbkNew1.SaveAs FileFormat:=xlExcel9795
wbkNew1.Close savechanges:=False
'If not changing fileformat then use:
' wbkNew1.Close savechanges:=True
'Release the Excel Application
'Close in proper order
Set wksNew1 = Nothing
Set wbkNew1 = Nothing
appEx.Quit
Set appEx = Nothing
End Sub
'*****Revised Code Stops********
Jim Cone
San Jose, CA
***************************************************
Quote:
> To All,
> I am working on an Accesst-to-Excel automation process and I am having
> difficulty in getting the Excel application to fully close (i.e. I can
> open up the Task Manager window and see the application running).
> Closing Access will force Excel to close, so I know that Access is
> holding on to the app.
> Here is a snip of the code:
> ****Code Starts Here****
> 'Some Variables Defined
> Dim dblocal As Database
> Dim appEx As Object
> Dim wbkNew1 As Excel.Workbook
> Dim wksNew1 As Excel.Worksheet
> 'Open the Excel Application
> Set appEx = CreateObject(Class:="Excel.Application")
> appEx.Workbooks.Open FileName:="c:\Export.xls"
> appEx.Worksheets("Export1").Activate
> appEx.Visible = False
> 'Selecting and Copying my Range
> With appEx.ActiveSheet.Range("R2")
> .Select
> .FormulaR1C1 = "=RC[-14]&RC[-2]&RC[-13]"
> .Copy
> End With
> With appEx.ActiveSheet.Range("R2:R150")
> .Select
> ActiveSheet.Paste
> .Copy
> .PasteSpecial Paste:=xlValues, Operation:=xlNone,
> skipblanks:=False, Transpose:=False
> End With
> 'I added this thinking it may work, but it has not
> appEx.CutCopyMode = False
> 'Deleted Some Unneeded Columns
> appEx.ActiveSheet.Columns("S").Delete Shift:=xlToLeft
> appEx.ActiveSheet.Columns("O:P").Delete Shift:=xlToLeft
> 'Trying to Release the Excel Application
> appEx.DisplayAlerts = False
> appEx.ActiveWorkbook.SaveAs FileName:="c:\Export.xls",
> FileFormat:=xlExcel9795, CreateBackup:=False
> appEx.ActiveWorkbook.Close
> appEx.DisplayAlerts = True
> appEx.Application.Quit
> appEx.Quit
> Set appEx = Nothing
> Set dblocal = Nothing
> Set wksNew1 = Nothing
> Set wbkNew1 = Nothing
> *****Code Stops********
> I have had success in automating Access-to-Excel Apps without the
> copying and pasting (just formatting). However, my attempts at
> integrating a copy/paste combination have not worked.
> What am I missing from the above code to get the Access application to
> release Excel? Is there something retained in the buffer?
> Any suggestions would be GREATLY appreciated.
> Thanks,
> Windman