MS Cmn Dialog Box openig directories only

Posted on 2001-06-06
Last Modified: 2010-05-02
I want my ms common dialog box to return the directory selected, rather than force me to select a file. How can I do that, is there a flag or something I can apply?

Question by:NiTech
LVL 10

Expert Comment

ID: 6159134
Yeah. The "Common Dialog Replacement Project" is just the place to look for:

The one you'll be interested in is the BrowseDialog. I personally prefer the server over the OCX.

It has the control, plus code samples!
LVL 10

Expert Comment

ID: 6159137
Actually, the OCX has been discontinued. :o)
LVL 43

Expert Comment

ID: 6159138
Or you can use the API SHBrowseForFolder dialog instead:

Private Type 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
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Sub Form_Load()
    'KPD-Team 1998
    Dim iNull As Integer, lpIDList As Long, lResult As Long
    Dim sPath As String, udtBI As BrowseInfo

    With udtBI
        'Set the owner window
        .hWndOwner = Me.hWnd
        'lstrcat appends the two strings and returns the memory address
        .lpszTitle = lstrcat("C:\", "")
        'Return only if the user selected a directory
    End With

    'Show the 'Browse for folder' dialog
    lpIDList = SHBrowseForFolder(udtBI)
    If lpIDList Then
        sPath = String$(MAX_PATH, 0)
        'Get the path from the IDList
        SHGetPathFromIDList lpIDList, sPath
        'free the block of memory
        CoTaskMemFree lpIDList
        iNull = InStr(sPath, vbNullChar)
        If iNull Then
            sPath = Left$(sPath, iNull - 1)
        End If
    End If

    MsgBox sPath
End Sub

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 10

Expert Comment

ID: 6159144
Here's the "programmer's reference"


Your example works, but I found it very finicky to customize it to behave exactly the way you want it. With the CCRP solution that's made very easy.
LVL 28

Accepted Solution

Ark earned 50 total points
ID: 6172731

Const BIF_EDITBOX = &H10

Private Sub Command1_Click()
  Dim oShell As Object
  Set oShell = CreateObject("Shell.Application")
  On Error Resume Next
  Caption = oShell.BrowseForFolder(0, "Choose a folder", BIF_RETURNONLYFSDIRS)
  If Err Then Caption = "Nothing selected"
  Set oShell = Nothing
End Sub


Author Comment

ID: 6455998
Thanks, sorry for late response.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question