从MS Access我生成了几个MS Access工作簿。通过以下代码,我获得了所有工作簿所需的保存位置。几天前,以下代码无问题。现在它突然失败,没有错误编号。 MS Access崩溃,我收到重新启动MS Access的提示,并自动创建我正在处理的MS Access项目的备份文件。
奇怪的是,如果我使用调试器逐步执行它,代码工作正常。它根本没有全速运转。
更新1:
如果我做了下降save_location
来电。
Private Sub make_report()
' TODO#: Change to late binding when working
Dim strSaveLocation as string
Dim xl as Excel.Application
dim wb as Excel.Workbook
strSaveLocation = save_location("G:\Group2\Dev\z_report")
Set xl=New Excel.Application
' do workbook stuff
With xl
strSaveLocation = strSaveLocation & "\report_name.xlsx"
wb.SaveAs strSavelLocation, xlOpenXMLWorkbook
End With ' xl
Set xl=nothing
End Sub
如果我像这样调用save_location
函数,它会突然崩溃MS Access。它不会抛出错误或任何东西。它只是崩溃了。
Private Sub make_report()
' TODO#: Change to late binding when working
Dim strSaveLocation as string
Dim xl as Excel.Application
dim wb as Excel.Workbook
Set xl=New Excel.Application
' do workbook stuff
With xl
' the call to save_location is inside of the xl procedure
strSaveLocation = save_location("G:\Group2\Dev\z_report")
strSaveLocation = strSaveLocation & "\report_name.xlsx"
wb.SaveAs strSavelLocation, xlOpenXMLWorkbook
End With ' xl
Set xl=nothing
End Sub
通过在save_location
工作字符串中移动Excel.Application
调用,它会失败。我不明白为什么。
Private Function save_location(Optional ByVal initialDir As String) As String
On Error GoTo err_trap
Dim fDialog As Object
Dim blMatchIniDir As Boolean
Set fDialog = Application.FileDialog(4) ' msoFileDialogFolderPicker
With fDialog
.Title = "Select Save Location"
If NOT (initialDir=vbnullstring) then
.InitialFileName = initialDir
End If
If .Show = -1 Then
' item selected
save_location = .SelectedItems(1)
End If
End With
Set fDialog = Nothing
exit_function:
Exit Function
err_trap:
Select Case Err.Number
Case Else
Debug.Print Err.Number, Err.Description
Stop
Resume
End Select
End Function
答案 0 :(得分:0)
虽然我不确定这是否是具体问题 - 但如果是这样的话,它会与任何VBA混淆。检查任何撇号的文件夹名称和文件名。虽然Windows允许这样做,但是在VBA中会将撇号视为注释,并且会使其崩溃。让客户端引导您完成他选择的确切文件,以确认文件名或文件夹名称中没有撇号字符。