Chi Is Current
asked on
How to Close Excel AND the Template File with Access VBA
The following segment within a sub runs! (good news)
However, I am not finding a way of closing Excel afterwards.
1) I can see multiple instances of Excel running in Task Manager: Processes
2) I am asked if I would like save changes to: OrderTMPL1.xlt after Access is closed, WHEN I SHUT DOWN THE COMPUTER
However, I am not finding a way of closing Excel afterwards.
1) I can see multiple instances of Excel running in Task Manager: Processes
2) I am asked if I would like save changes to: OrderTMPL1.xlt after Access is closed, WHEN I SHUT DOWN THE COMPUTER
Dim xlObj As Object
Dim xlApp As Object
Dim xltPath As String
Dim sFile as String 'Path and filename set within 'msoFileDialogSaveAs'
...
xltPath = "Q:\OrderTMPL.xlt"
Set xlObj = CreateObject("Excel.Application")
xlObj.Workbooks.Add (xltPath)
With xlObj
.Range("F1") = vDestination
.Range("F2") = vDate
.Range("F3") = vDateRet
.Range("F4") = vcount
.Range("F5") = vFC
.Range("A8").CopyFromRecordset rs1
End With
xlObj.ActiveWorkbook.SaveAs sFile
'ActiveWorkbook.Close False
'Application.QUIT
Set xlObj = Nothing
Set xlApp = Nothing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Gustav and GoFlow ~ Clearly I need to develop a better facility with the language and syntax.
Thank you both!!!
Jacob
Thank you both!!!
Jacob
You are welcome!
/gustav
/gustav
You can use GetObject instead of CreateObject to avoid creating multiple instances of Excel (or Word, etc.). Here is some boilerplate 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
ASKER
Thank you, Helen! Sounds like a great idea! ~ Jacob
ASKER
That Works!
So, there is a second related question:
https://www.experts-exchange.com/questions/28920520/Using-Application-FileDialog-msoFileDialogSaveAs-function-How-can-I-skip-Create-File-in-in-main-function.html