1

I don't see any parameter in GetOpenFilename to set default folder to Downloads.

Currently, it opens Documents folder. Is it possible to make the default location as Downloads folder.

I can't hardcode the path as Downloads without including the Username. e.g 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
newbie
  • 27
  • 5
  • Here is how to get the download dir location: https://stackoverflow.com/questions/23070299/get-the-windows-download-folders-path - call `chdir` with this path before you call `GetOpenFileName()`. – Alex K. Aug 26 '22 at 10:33
  • How do I direct GetOpenFilename to open the path, GetDownloadsPath = Environ$("USERPROFILE") & "\Downloads" – newbie Aug 26 '22 at 11:50

1 Answers1

1

Choose Files to Open (FileDialog)

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
VBasic2008
  • 44,888
  • 5
  • 17
  • 28
  • How to adapt below code with the above. FileToOpen = Application.GetOpenFilename(FileFilter:="CSV Filter(*.csv),*.csv", Title:="Open CSV file") If FileToOpen = False Then Do ans = MsgBox("No file selected. Cannot continue.", 53, "Try again") If ans = 2 Then Exit Do FileToOpen = Application.GetOpenFilename(FileFilter:="CSV Filter(*.csv),*.csv", Title:="Open CSV file") Loop If ans = 2 Then MsgBox "No file selected. User cancelled.", vbInformation, "Special" Exit Sub Else Workbooks.Open FileToOpen – newbie Aug 26 '22 at 22:40
  • I managed to merge it. Updated my original post. – newbie Aug 27 '22 at 09:33