选择要打开的文件(
我在GetOpenFilename中没有看到任何将默认文件夹设置为Downloads的参数。
当前,它打开Documents文件夹。是否可以将默认位置设置为下载文件夹。
如果不包括用户名,我无法将路径硬编码为下载。例如C:\Users\NameOfUser\Downloads
**********Solution**********
Dim FilePaths As FileDialogSelectedItems
Dim iFolderPath As String
iFolderPath = Environ("USERPROFILE") _
& Application.PathSeparator & "Downloads" & Application.PathSeparator
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.Filters.Clear
.Filters.Add "Open CSV", "*.csv"
.InitialFileName = iFolderPath
If .Show <> -1 Then
Do
ans = MsgBox("No file selected. Cannot continue.", 53, "Try again")
If ans = 2 Then Exit Do
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.Filters.Clear
.Filters.Add "Open CSV", "*.csv"
.InitialFileName = iFolderPath
.Show
End With
Loop
If ans = 2 Then MsgBox "No file selected. User cancelled.", vbInformation, "Special"
Exit Sub
End If
Set FilePaths = .SelectedItems
End With
Dim FilePath As Variant
For Each FilePath In FilePaths
Workbooks.Open FilePath
Next FilePath
选择要打开的文件(FileDialog
(
- 这里有一种使用
FileDialog object
的不同方式 - 使用
Environ function
构建初始路径
Sub ChooseFilesToOpen()
Dim iFolderPath As String: iFolderPath = Environ("USERPROFILE") _
& Application.PathSeparator & "Downloads" & Application.PathSeparator
Dim FilePaths As FileDialogSelectedItems
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.Filters.Clear
.Filters.Add "Excel Workbook", "*.xlsx"
.InitialFileName = iFolderPath
If .Show <> -1 Then
MsgBox "Canceled.", vbExclamation
Exit Sub
End If
Set FilePaths = .SelectedItems
End With
Dim FilePath As Variant
For Each FilePath In FilePaths
Debug.Print FilePath
Next FilePath
End Sub