Browse for folder (shell32)

bandit_1981

Board Regular
Joined
Aug 17, 2005
Messages
201
I am using this same chunk of code i think most of this board uses but i need a slight change to it. I do not want the user to be able to select anything until they close the dialog. As it is now they can choose the user form that has the button that displays it. IE Excel -> UserForm -> Dialog Box. User are very click happy and will certainly open many dialogs before they stop clicking.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
What code are you using?

What do you actually want to do?
 
Upvote 0
Code:
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
    bInfo.hOwner = 0
'   Root folder = Desktop
    bInfo.pidlRoot = 0&
'   Title in the dialog
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder."
    Else
        bInfo.lpszTitle = Msg
    End If
'   Type of directory to return
    bInfo.ulFlags = &H1
'   Display the dialog
    x = SHBrowseForFolder(bInfo)
'   Parse the result
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal x, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetDirectory = Left(path, pos - 1)
    Else
        GetDirectory = ""
    End If

What i want it todo is keep focus on the dialog box until the dialog box is closed out. As it works now you can click on level back and keep launching the dialog.
 
Upvote 0
Could you actually post all the code?

As far as I know if you open a dialog it will have focus until it's closed.
 
Upvote 0
Code:
'32-bit API declaration
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
  Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Declare Function GetSystemMetrics Lib "user32" _
  (ByVal nIndex As Long) As Long

Public Type BROWSEINFO
    hOwner As Long
    pidlRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfn As Long
    lParam As Long
    iImage As Long
End Type


Code:
Private Sub cmdBackupLoc_Click()
    On Error Resume Next
    'allow the user to select the folder to backup to
    Call GetAFolder(0, "Please select a location for the backup.")
End Sub

Code:
Sub GetAFolder(ByVal intOpt As Long, ByVal vmessage As String)
On Error Resume Next
Dim Msg As String
Dim UserFile As String
    Msg = vmessage
    UserFile = GetDirectory(Msg)
    If UserFile = "" Then
        MsgBox "No folder selected"
    Else
        If intOpt = 0 Then
            frmBackUp.lblpath.Caption = UserFile
        Else
            frmBackUp.lblrestoreloc.Caption = UserFile
        End If
    End If
End Sub

Code:
Function GetDirectory(Optional Msg) As String
On Error Resume Next
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
    bInfo.hOwner = 0
'   Root folder = Desktop
    bInfo.pidlRoot = 0&
'   Title in the dialog
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder."
    Else
        bInfo.lpszTitle = Msg
    End If
'   Type of directory to return
    bInfo.ulFlags = &H1
'   Display the dialog
    x = SHBrowseForFolder(bInfo)
'   Parse the result
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal x, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetDirectory = Left(path, pos - 1)
    Else
        GetDirectory = ""
    End If

End Function
 
Upvote 0
Sorry I was wrong.:oops:

You can get go back to the userform.

Here's some code I used to let the user browse for a folder when they click a command button on a userform.
Code:
Private Sub CommandButton1_Click()
    CommandButton1.Enabled = False
    MsgBox GetFolder
End Sub

Private Function GetFolder() As String
'common UDF for getting a folder name
    Dim ff As Object
    Set ff = CreateObject("Shell.Application"). _
             BrowseForFolder(0, "Please select a folder", 0, "c:\\")
    If Not ff Is Nothing Then
        GetFolder = ff.Items.Item.Path
    Else
        GetFolder = vbNullString
    End If
    CommandButton1.Enabled = True
End Function

Obviously it uses a different method to show the dialog but perhaps you could use the same idea ie enabling/disabling the command button.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,485
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top