在 VBA 中为 FileDialog 默认文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16917122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Defaulting a folder for FileDialog in VBA
提问by Chuck
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
How to make the FileDialog default to a specific folder when it initially opens?
最初打开时如何使 FileDialog 默认为特定文件夹?
回答by Chuck
Add the folder path (including the trailing \
) to InitialFileName
. For example, to open the dialog in the user's home directory do:
将文件夹路径(包括结尾的\
)添加到InitialFileName
. 例如,要在用户的主目录中打开对话框,请执行以下操作:
f.InitialFileName = Environ("USERPROFILE") & "\"
If you forget the trailing \
,
then the dialog box will still open in the correct folder,
but the folder name will also appear as the default selected file name.
Then the dialog will be looking for a sub-folder with the same name, which usually doesn't exist.
如果您忘记了尾随\
,则对话框仍会在正确的文件夹中打开,但文件夹名称也将显示为默认选择的文件名。然后对话框将寻找一个具有相同名称的子文件夹,该子文件夹通常不存在。
回答by Costas Petrides
I tried ChDir "your\path\here" and it works
我试过 ChDir "your\path\here" 并且它有效
回答by KathyM
Or, before opening the dialog, simply change the deault file directory with:
或者,在打开对话框之前,只需使用以下命令更改默认文件目录:
Application.Options.DefaultFilePath(wdDocumentsPath) = "your\path\here"