The function below opens an excel spreadsheet, does basic formatting, then closes the spreadsheet. For some reason, Excel is staying open until I exit the database. I can't figure out why it is not closing.
Function ExportExcel(strFileName As String, strQry As String)
'Export query data into Excel spreadsheet
If Dir(Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx") <> "" Then
Kill Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx"
DoCmd.TransferSpreadsheet acExport, 10, strQry, Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx", True, ""
Else
DoCmd.TransferSpreadsheet acExport, 10, strQry, Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx", True, ""
End If
'Create Excel Object, manipulate newly saved spreadsheet
'Create Object variables
Dim xlApp As Object
Dim wbExcel As Object
'Set Excel Object
Set xlApp = CreateObject("Excel.Application")
'Set Object containing opened strFileName spreadsheet
Set wbExcel = xlApp.Workbooks.Open(Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx")
'Manipulate spreadsheet file
'Autofit Columns
wbExcel.Sheets(1).Columns.AutoFit
'Set column headings to bold typeface
wbExcel.Sheets(1).Rows(1).Font.Bold = True
'Set name of worksheet to Sheet1
wbExcel.Sheets(1).Name = "Sheet1"
'Leave cursor at cell A1
Range("A1").Select
'Close and Save Workbook
xlApp.ActiveWorkbook.Close (True)
'Close Excel Object
xlApp.Quit
'Clear Object variables
Set wbExcel = Nothing
Set xlApp = Nothing
Thank you in advance for your help with this problem!!