我有一個VBA模塊,用於打開Excel工作簿並將數據從Access查詢複製到Excel工作簿中的單元格。一切正常,直到我關閉Excel工作簿。當發生這種情況時,我得到錯誤50290,91和424.這些錯誤似乎是隨機發生的,並且模塊崩潰的代碼行從不相同。它似乎隨機崩潰。在關閉Excel工作簿時在MS Access 2010中獲取錯誤50290,91和424
這裏是模塊的第一部分,這裏的一切都定義:
Dim RecSet As DAO.Recordset
Dim objXLAppln As Excel.Application
Dim objWBook As Excel.Workbook
Dim i As Integer
Dim j As Integer
Dim StrPathFile As String, strFile As String, strPath As String
Dim strBrowseMsg As String, strInitialDirectory As String, strFilter As String
'show dialogue box
strBrowseMsg = "Select the EXCEL file:"
'set directory to load files from
strInitialDirectory = "C:\Bridge_CIP_Part-A_B\"
'run strFilter function
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xlsx)", "*.xlsx")
StrPathFile = ahtCommonFileOpenSave(InitialDir:=strInitialDirectory, _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:=strBrowseMsg, _
Flags:=ahtOFN_HIDEREADONLY)
If StrPathFile = "" Then
MsgBox "No file was selected.", vbOK, "No Selection"
Exit Function
End If
'Set Excel application object. Critical for macro to run properly. Do not change.
Set objXLAppln = New Excel.Application
'Open workbook and worksheet to load data.
With objXLAppln
Set objWBook = .Workbooks.Open(StrPathFile)
objXLAppln.Visible = True
End With
Set RecSet = CurrentDb.OpenRecordset("Part_A-B Query")
這裏是在最後的代碼塊,我收的一切:
'Close everything
RecSet.Close
objWBook.Close SaveChanges:=False
objXLAppln.Quit
Set RecSet = Nothing
Set objWBook = Nothing
Set objXLAppln = Nothing
如何獲取代碼關閉工作簿而不會崩潰?