Link to home
Start Free TrialLog in
Avatar of Chi Is Current
Chi Is CurrentFlag for United States of America

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

    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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chi Is Current

ASKER

Gustav and GoFlow ~ Clearly I need to develop a better facility with the language and syntax.

Thank you both!!!

Jacob
You are welcome!

/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

Open in new window

Thank you, Helen! Sounds like a great idea! ~ Jacob