首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GetOpenFilename默认下载位置

GetOpenFilename默认下载位置
EN

Stack Overflow用户
提问于 2022-08-26 10:15:47
回答 1查看 88关注 0票数 1

我在GetOpenFilename中没有看到任何参数来将默认文件夹设置为下载。

当前,它打开“文档”文件夹。是否可以将默认位置设置为“下载”文件夹。

如果没有用户名,我就无法将路径硬编码为下载。例如C:\Users\NameOfUser\下载

**********Solution**********

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-26 11:43:41

选择要打开的文件(FileDialog)

这里有一种使用FileDialog object.

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73499444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档