Function SendToExcel(strTQName As String, strSheetName As String)
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As DAO.Field
Dim lngMaxRow As Long
Dim lngMaxCol As Long
Dim lngLastRow As Long
Dim N As String
Dim strPath As String
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
Const xlUp As Long = -4162
Const xlDown As Long = -4121
On Error GoTo Err_Handler
strPath = "E:\Payroll Test\Template\4WeeklyMasterTemplate.xls"
Set rst = CurrentDb.OpenRecordset(strTQName)
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(strPath)
'Moved to end so Excel doesn't open before the report is finished rendering
'ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets(strSheetName)
rst.MoveFirst
xlWSh.Range("C8").CopyFromRecordset rst
xlWSh.Range("D8").CopyFromRecordset rst
' selects the first cell to unselect all cells
xlWSh.Range("A1").Select
rst.Close
Set rst = Nothing
'Remove prompt to save file
ApXL.DisplayAlerts = False
xlWBk.SaveAs "E:\Payroll Test\FourWeeksEnding" & Date & ".xlsx", 51
ApXL.DisplayAlerts = True
'Open after report is completes
ApXL.Visible = True
'ApXL.Quit
Exit Function
Err_Handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
ApXL.Quit
Set ApXL = Nothing
Exit Function
End Function