Display the Browse For Folder Dialog


Author: Gilles ROY

Browse Folders Dialog

This code allows you to dispay the Browse Folders dialog with one simple function call. The function returns the name of the selected folder, or an empty string if cancel was pressed.

Screen Shot

Paste the below code straight into a BAS module, or download the BAS file from the below link.


'###############################################################################
'# $FICHIER : BrowseFolders.bas
'# $VERSION : v1.0
'#
'# HISTORI/UE DES MODIFICATIONS/
'#  v1.0   OD   14/05/1999  Criation
'###############################################################################
option Explicit

'Constantes
private Const BIF_RETURNONLYFSDIRS = &H1        'Uniquement des ripertoire
private Const BIF_DONTGOBELOWDOMAIN = &H2       'Domaine globale intredit
private Const BIF_STATUSTEXT = &H4              'Zone de saisie autorisie
private Const BIF_RETURNFSANCESTORS = &H8
private Const BIF_EDITBOX = &H10                'Zone de saisie autorisie
private Const BIF_VALIDATE = &H20               'insist on valid result (or CANCEL)
private Const BIF_BROWSEFORCOMPUTER = &H1000    'Uniquement des PCs.
private Const BIF_BROWSEFORPRINTER = &H2000     'Uniquement des imprimantes
private Const BIF_BROWSEINCLUDEFILES = &H4000   'Browsing for Everything

private Const MAX_PATH = 260

'Types
private Type T_BROWSEINFO
   HwndOwner      as Long
   pIDLRoot       as Long
   pszDisplayName as Long
   lpszTitle      as Long
   ulFlags        as Long
   lpfnCallback   as Long
   lParam         as Long
   iImage         as Long
End Type

'Fonctions API Windows
private Declare Function SHBrowseForFolder Lib "shell32" _
                                  (lpbi as T_BROWSEINFO) as Long

private Declare Function SHGetPathFromIDList Lib "shell32" _
                                  (byval pidList as Long, _
                                  byval lpBuffer as string) as Long

private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" _
                                  (byval lpString1 as string, byval _
                                  lpString2 as string) as Long

'*************************************************************
'*  BrowseFolder :
'*  Entries :   - HwndOwner     : Handle de la fenjtre appelante
'*              - Titre         : Titre
'*  Sorties :
'*              - string contenant le chemin complet ou Chaine vide
'*                (si annulation)
'*
'*  Affiche une boite de dialogue permettant la silection d'un ripertoire.
'*  Renvoie une chaine vide si l'opirateur annule.
'*************************************************************
public Function BrowseFolder(byval HwndOwner as Long, _
    byref Titre as string) as string

    Dim lpIDList as Long
    Dim sBuffer as string
    Dim BrowseInfo as T_BROWSEINFO

    'Initialise l'affichage
    BrowseFolder = ""
    With BrowseInfo
        .HwndOwner = HwndOwner
        .lpszTitle = lstrcat(Titre, "")
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With

    'Affiche la boite de dialogue
    lpIDList = SHBrowseForFolder(BrowseInfo)
    
    'Ricuphre le ripertoire silectionni
    If (lpIDList) then
        sBuffer = Space(MAX_PATH)
        SHGetPathFromIDList lpIDList, sBuffer
        sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
        BrowseFolder = sBuffer
    End If
End Function




Download BrowseFolders.Bas



Comments

  • Create a New folder button does not appear on Some Win2000 and Win 98

    Posted by Jim Bassett on 11/05/2004 03:32pm

    The 'Create a New Folder" button located at the lower left corner of the browse for folder does not appear on some Winsows 2000 and Windows 98 PC. All of these PCs have IE 5.5 or 6.0

    Reply
  • How to create a new folder in the browse folder?

    Posted by Legacy on 05/30/2003 12:00am

    Originally posted by: zhang jinghua

    When I use the brose folder, and if I want to create a new subfolder in the selected folder,but when I right-click the folder,it just pops up a "what's this?"menu, so can I add a "new"folder menu or add icon-which can make me add new subfolder?

    • Certainly

      Posted by xingsy on 04/08/2004 11:24am

      Please add the following: Private Const BIF_USENEWUI = &H40 .ulFlags = BIF_USENEWUI + BIF_RETURNONLYFSDIRS '+ BIF_STATUSTEXT

      Reply
    Reply
  • Great code

    Posted by Legacy on 04/01/2003 12:00am

    Originally posted by: Greg

    Very nice piece! Thanks!

    Reply
  • pelicansolutions.com

    Posted by Legacy on 01/30/2001 12:00am

    Originally posted by: Kurz

    Does anybody know a way to capture the browse for folder window into the listbox portion of a combobox?

    thank you very much!

    Reply
  • Display the Browse For Folder Dialog

    Posted by Legacy on 12/05/2000 12:00am

    Originally posted by: Anil

    This is the excellent piece of code I have ever seen.
    

    Reply
  • VB CodeGuru: Display the Browse For Folder Dialog

    Posted by Legacy on 05/23/2000 12:00am

    Originally posted by: Angelo Fiorillo

    GREAT !!!!

    Reply
  • SHBrowseForFolder

    Posted by Legacy on 05/16/2000 12:00am

    Originally posted by: saul

    I found "a long time ago" visual basic code using the SHBrowseForFolder api. In that code there were the possibility to call SHBrowseForFolder with a parameter indicating the initial path. Here is the istantiation of this parameter:

    .
    .
    .
    With BI
    .hOwner = m_hWnd 'parent's handle
    .pidlRoot = SHSimpleIDListFromPath(vFolder) 'Root node
    .lpszTitle = strMsg
    .ulFlags = lFlag
    '>>> INITIAL FOLDER <<<
    lInitial = LocalAlloc(LPTR, Len(sInitial))
    Call CopyMemory(ByVal lInitial, ByVal sInitial, Len(sInitial))
    .lParam = lInitial
    .lpfn = ProcAddress(AddressOf BrowseCallbackProc)
    .pszDisplayName = Space(MAX_PATH)
    End With
    .
    .
    .

    Where BI is the BROWSEINFO structure to pass to SHBrowseForFolder api.
    The sample above is a little bit more complex and complete respect to the one I found in your site. I tested "my" code but I found that the "initial path parameter" does not function always!

    Reply
  • Display the Browse For Folder Dialog - English Version ?

    Posted by Legacy on 01/20/2000 12:00am

    Originally posted by: Heiachi

    Hi,
    Is there any english version for this code ?

    thnanks, H

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • It's time high-level executives and IT compliance officers recognize and acknowledge the danger of malicious insiders, an increased attack surface and the potential for breaches caused by employee error or negligence. See why there is extra emphasis on insider threats.

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds