在VBA中为FileDialog设置默认文件夹

在VBA中为FileDialog设置默认文件夹,vba,filedialog,Vba,Filedialog,如何在文件对话框最初打开时将其默认为特定文件夹?将文件夹路径(包括尾随的\)添加到初始文件名。例如,要打开用户主目录中的对话框,请执行以下操作: Private Sub Command93_Click() Dim f As Object Dim strFile As String Dim strFolder As String Dim varItem As Variant Dim P As String Dim DeleteEverything

如何在文件对话框最初打开时将其默认为特定文件夹?

将文件夹路径(包括尾随的
\
)添加到
初始文件名
。例如,要打开用户主目录中的对话框,请执行以下操作:

Private Sub Command93_Click()

    Dim f As Object
    Dim strFile As String
    Dim strFolder As String
    Dim varItem As Variant
    Dim P As String
    Dim DeleteEverything As String

        DoCmd.SetWarnings False
        DeleteEverything = "DELETE * FROM [TABLE]"
        DoCmd.RunSQL DeleteEverything

    Set f = Application.FileDialog(3)
    f.AllowMultiSelect = False
    If f.Show Then
        For Each varItem In f.SelectedItems
            strFile = Dir(varItem)
            strFolder = Left(varItem, Len(varItem) - Len(strFile))
            P = strFolder & strFile
        Next
    End If
    Set f = Nothing

        DoCmd.TransferText acImportFixed, "[IMPORT SPECIFICATION]", "[TABLE]", P, False

End Sub
如果您忘记了尾随的
\
, 然后对话框仍将在正确的文件夹中打开, 但文件夹名称也将显示为默认的选定文件名。
然后,该对话框将查找具有相同名称的子文件夹,该子文件夹通常不存在

或者,在打开对话框之前,只需使用以下命令更改deault文件目录:

f.InitialFileName = Environ("USERPROFILE") & "\"

我尝试了ChDir“your\path\here”,它可以工作

查看这里提供的代码:此代码仅适用于MS Word,OP询问的是MS Access。我认为
应用程序将使用PathSeparator
而不是
“\”
better@johnc.j. 你为什么这么说?“\”是路径seperator@Davesexcel你好像在用窗户。在macOS和Linux上,它将是“/”<代码>应用程序。PathSeparator将在任何位置工作。请访问并检查。
Application.Options.DefaultFilePath(wdDocumentsPath) = "your\path\here"