26
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

如何使 FileDialog 在最初打开时默认为特定文件夹?

4

3 回答 3

60

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

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

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

于 2013-06-04T12:43:03.817 回答
0

我试过 ChDir "your\path\here" 并且它有效

于 2020-02-12T12:21:58.687 回答
-1

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

Application.Options.DefaultFilePath(wdDocumentsPath) = "your\path\here"
于 2016-06-17T15:47:36.770 回答