I have to export a query (the entire table) to a existing workbook which has a worksheet which needs to be overwritten. Why do I get this error?
Code:
Dim rst As 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 strPath As String
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
On Error GoTo Err_Handler
strPath = CurrentProject.Path & "\Qry_V.xlsx"
Set rst = CurrentDb.OpenRecordset("Qry_V")
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(strPath)
Set xlWSh = xlWBk.Worksheets(Qry_V)
rst.MoveFirst
xlWSh.CopyFromRecordset rst
xlWSh.SELECT
'xlWSh.Activate
'xlWSh.Cells.rows(7).AutoFilter
'xlWSh.Cells.rows(7).EntireColumn.AutoFit
rst.Close
Set rst = Nothing
'Remove prompt to save file
ApXL.DisplayAlerts = False
xlWBk.SaveAs CurrentProject.Path & "\Qry_V.xlsx", 51
ApXL.DisplayAlerts = True
'Open after report is completes
ApXL.Visible = True
'ApXL.Quit
Exit Sub
Err_Handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Sub