使用CurrentProject.Path作为FileDialog中的默认文件夹时出现“路径不存在”错误

时间:2014-02-11 17:11:08

标签: vba ms-access access-vba

所以我已经编写(借用了)一些代码来自动导出,下面是我正在使用的folderpicker dialoge,好奇地使用currentproject.path它在正确的初始目录中启动,但是说路径不存在,直到你导航然后回到文件夹。我认为这与选择器没有返回值之前有关,直到你在某处或某处导航?

这只是一个问题,因为我希望用户能够指定他们想要保存导出的位置,包括项目的当前路径。有什么想法吗?

Function GetFolder(Optional startFolder As Variant = -1) As Variant
    Dim fldr As FileDialog
    Dim vItem As Variant
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)

    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
            If startFolder = -1 Then
           .InitialFileName = CurrentProject.Path
        Else
            If Right(startFolder, 1) <> "\" Then
                .InitialFileName = startFolder & "\"
            Else
                .InitialFileName = startFolder
            End If
        End If
        If .Show <> -1 Then GoTo NextCode
        vItem = .SelectedItems(1)
    End With

NextCode:
    GetFolder = vItem
    Set fldr = Nothing
    Debug.Print GetFolder
End Function

1 个答案:

答案 0 :(得分:3)

将\添加到默认文件夹,它不会突出显示。

例如:

 .InitialFileName = CurrentProject.Path & "\"

请注意,他们可以“击中”确定。