Select folder using browse button in VBA

Viewed 43

I have a requirement where I need to select a folder instead of a file, while clicking a browse button. The code to select a file, while clicking the browse button will be as below.

Case "Browse"
DlgText "path", GetFilePath(,"*.*","C:\","Open sheet")

How to change this to select a folder, instead of a file.

Thanks in advance

1 Answers

Please, try the next way:

Case "Browse"
  Dim folderPath As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select the necessary folder!"
        .Show
        .AllowMultiSelect = False
        If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
            MsgBox "You did not select any folder..."
            Exit Sub
        End If
        folderPath = .SelectedItems(1)
    End With
  'your existing code...

Edited:

If you work in Windows OS, even if ERStudio, which I am not fammiliar to, does not expose such a method, please try the next way, using VBScript objects:

Case "Browse"
   Dim objSh As Object, objFold As Object, strStartFolder As String, strFolder As String
   
    strStartFolder = "C:\" 'you can use here what starting folder you want
    Set objSh = CreateObject("Shell.Application")
    Set objFold = objSh.BrowseForFolder(0, "Select The necessary Folder", 0, strStartFolder)
    If IsObject(objFold) Then strFolder = objFold.Self.Path
    MsgBox strFolder 'use it where necessary and comment this testing line...
'your existing code...
Related