Sub exportExpense()
'Step 1: Declare variables
Dim lngColumn As Long
Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim blnEXCEL As Boolean, blnHeaderRow As Boolean
Dim curTotalExpense As Currency
curTotalExpense = Nz(DLookup("FeesTotal", "qryExpenseExportTotal"), 0)
blnEXCEL = False
blnHeaderRow = True
'Step 2: Create Excel Application Object
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
On Error GoTo 0
'Set Excel to visible while code is running
xlx.Visible = True
'Step 3: declare actual path and filename of the EXCEL
'file into which you will write the data
Set xlw = xlx.Workbooks.Open("P:\Design2147 Database\Reports\Expense Report.xltm")
'Step 4: Set WorksheetName with the actual
'name of the worksheet in the EXCEL file
Set xls = xlw.Worksheets("Expense List")
'Step 5: Set cell reference into which the
'first data value is to be written
Set xlc = xls.Range("A2")
Set dbs = CurrentDb()
'Step 6: Set name of the table or query
'whose data are to be written into the worksheet
Set rst = dbs.OpenRecordset("qryExpenseExport", dbOpenDynaset, dbReadOnly)
'Step 7: Copy recordset tospreadsheet
If rst.EOF = False And rst.BOF = False Then
rst.MoveFirst
xlc.CopyFromRecordset rst
'\\add some formatting
xlx.ActiveSheet.Cells.Select
xlx.ActiveSheet.Cells.EntireColumn.AutoFit
End If
'\\Sub Insert expense total into spreadsheet
GetLastRow xlc, curTotalExpense
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing
'Step 7: Clean up the EXCEL objects
Set xlc = Nothing
Set xls = Nothing
'xlw.Close False ' close the EXCEL file and save the new data
Set xlw = Nothing
'If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
End Sub