MS Access VBA文件对话框崩溃

时间:2017-09-12 14:10:38

标签: vba ms-access access-vba

从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
  • 已尝试的操作:
    • 反编译项目并重新编译
    • 创建新的MS Access项目并导入所有对象
    • 紧凑并修复
    • 重置所有参考
  • 注意:
    • 我正在使用客户端系统和
    • 我不知道有任何系统更新
    • 客户的系统是通过VMWare的虚拟桌面
    • Office 2013
    • Windows 7 Pro

1 个答案:

答案 0 :(得分:0)

虽然我不确定这是否是具体问题 - 但如果是这样的话,它会与任何VBA混淆。检查任何撇号的文件夹名称和文件名。虽然Windows允许这样做,但是在VBA中会将撇号视为注释,并且会使其崩溃。让客户端引导您完成他选择的确切文件,以确认文件名或文件夹名称中没有撇号字符。