2015-02-09 270 views
0

我有一個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 

如何獲取代碼關閉工作簿而不會崩潰?

回答

0

既然你不顯示該行是隨機的錯誤,我採取一般的立場,其中一些問題可能的原因:

參考圖書館 - 有可能是丟失或未知的庫引用。在運行Access應用程序的每臺計算機上檢查Alt + F11/Tools/References。此外,由於您運行的是Early Binding,請確保運行此數據庫的所有機器都具有使用正確版本選擇的Excel庫對象。另外,後期綁定Set objXLAppln = CreateObject('Excel.Application')可以避免跨機器的這種參考兼容性。

不同的Windows操作系統(Windows 7對8 VS 8.1,32位與64位)代替標準的Windows打開文件對話框的,考慮使用這證明是跨OS更穩定Application.FileDialog

Dim fd As Object 

Set fd = Application.FileDialog(msoFileDialogFilePicker) 

With fd 
    .Title = "Select the EXCEL file:" 
    .AllowMultiSelect = False 
    .Filters.Clear 
    .Filters.Add "Excel files (*.xlsx)", "*.xlsx" 
    .FilterIndex = 1 
    .InitialFileName = "C:\Bridge_CIP_Part-A_B\" 
    If .Show = True Then 
     strFilePath = .SelectedItems(1) 
    Else 
     'The user pressed Cancel. 
     MsgBox "No file Selected", vbExclamation 
     Set fd = Nothing 
     Exit Function 
    End If 
End With 

管理流程 您打開一個Excel對象到屏幕objXLAppln.Visible = True,輸出Access查詢到現有的工作簿,但不保存它SaveChanges:=False然後退出objXLAppln.Quit。這不是一個錯誤,而是調和這個過程。在你的錯誤處理,關閉了對象完全一樣的:

On Error Goto ErrHandle: 
... 
ErrHandle: 
    'Close everything 
    Msgbox Err.Number & " - " & Err.Description, vbCritical 
    RecSet.Close 
    objWBook.Close SaveChanges:=False 
    objXLAppln.Quit 
    Set RecSet = Nothing 
    Set objWBook = Nothing 
    Set objXLAppln = Nothing 
    Exit Function 

此外,一定要檢查Task Manager/Processes,看看舊的EXCEL.EXE實例不是由於以前的錯誤剩餘;只讀文件可能仍在內存中。最後,在重大VBA代碼更改和錯誤故障排除之後,decompile and compact您的數據庫。

祝你好運!

相關問題