Ich versuche, ein Excel-Arbeitsblatt mit der Ausgabe von einer Access-Abfrage zu überschreiben, die Access VBA verwendet. Ich habe einen großen Access VBA-Code von this post here abgegriffen und geändert. Die Aufgabe ist abgeschlossen, aber am Ende bekomme ich einen Laufzeitfehler:Laufzeitfehler 424 beim Überschreiben der Kalkulationstabelle aus Access VBA
Run-time Error '424': Object Required
Der geänderte Code ist unten. Irgendwelche Ideen, warum das passiert?
Option Compare Database
Option Explicit
Private Const strFilePath As String = "C:\Users\MyPC\Desktop\Test Tracker\APS_Timeline_Status.xlsm"
Private Const strTQName As String = "q_APS_Timeline_Status_For_Export"
Private Const strSheetName As String = "TimeLine_Status"
Sub Update_timeline_tracker()
SendTQ2XLWbSheet(strTQName, strSheetName, strFilePath).Run
End Sub
Public Function SendTQ2XLWbSheet(strTQName As String, strSheetName As String, strFilePath 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
' strFilePath is the name and path of the file you want to send this data into.
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As DAO.Field
Dim strPath As String
On Error GoTo err_handler
DoCmd.SetWarnings False
strPath = strFilePath
Set rst = CurrentDb.OpenRecordset(strTQName)
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(strPath)
Debug.Print strPath
ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets(strSheetName)
ApXL.DisplayAlerts = False
xlWSh.Activate
xlWSh.Range("A1").Select
For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("A2").CopyFromRecordset rst
' selects the first cell to unselect all cells
xlWSh.Range("A1").Select
rst.Close
xlWBk.SaveAs FileName:="C:\Users\MyPC\Desktop\Test Tracker\APS_Timeline_Status.xlsm"
xlWBk.Close
ApXL.Quit
Set rst = Nothing
Exit_SendTQ2XLWbSheet:
Exit Function
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Resume Exit_SendTQ2XLWbSheet
DoCmd.SetWarnings True
End Function
was ist, wenn Sie die 'privaten const' machen geschieht ein 'public const'? Sie sind außerhalb einer Prozedur, also würde ich denken, dass es öffentlich sein sollte – LiamH
Hi Liam. Ich habe alle privaten const in öffentliche geändert. Derselbe Laufzeitfehler wurde gefunden. – mccdo
Ich kann keine undefinierten Objekte sehen. Wenn Sie Ihre Fehlerbehandlung entfernen, in welcher Zeile erscheint Ihr Fehler? – LiamH