File and Folder Picker

Folder Picker

'FOLDER PICKER
Sub FolderPicker()
    Dim FolderPath As String
    Dim uMsg As String
    uMsg = vbNullString
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False 
        If .Show <> -1 Then
            uMsg = "No folder or file selected"
            GoTo endsub
        End If
        FolderPath = .SelectedItems(1)
    End With
    MsgBox FolderPath
endsub:
    If uMsg <> "" Then MsgBox uMsg
End Sub



File Picker

'FILE PICKER

Sub FilePicker()
    Dim flpth  As String
    'if you want to choose only Text files
    flpth = Application.GetOpenFilename("Textfiles (*.txt),*.txt", , "Open a textfile...")

    'if you want to choose only excel files
    flpth = Application.GetOpenFilename("Excel File (*.xls*)," & "*xls*")

    ' if you want to choose only word files
    flpth = Application.GetOpenFilename("Word File (*.doc*)," & "*doc*")

    ' If you want to filter file with multiple filetypes
    flpth = Application.GetOpenFilename("Image Files (*.bmp;*.gif;*.jpg;*.jpeg)," & "*bmp;*gif;*.jpg;*.jpeg")

    MsgBox flpth
End Sub

All Files From Folder

'EXAMPLE 1
Sub All_Files_From_Folder()

    Dim fso As Object
    Dim fso_Folder As Object
    Dim fso_file As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fso_Folder = mycomputer.GetFolder("F:\Rajan\Contacts\")
   
    For Each fso_file In fso_Folder.Files
        
        Debug.Print fso_file.Name

    Next

Set fso_Folder = Nothing
Set fso_file = Nothing
Set fso = Nothing

End Sub
'EXAMPLE 2
Sub All_Files_From_Folder_Method_2()
    Dim StrFile As String
    StrFile = Dir("E:\Dropbox\Excel\VBA TO EXCEL\")
    Do While Len(StrFile) > 0
        Debug.Print StrFile
        StrFile = Dir
    Loop
End Sub

Total Page Visits: 933 - Today Page Visits: 6

Leave a Reply