Browse for Folder using API "SHBrowseForFolder"

Hello there ....
I use the following code to browse for a Folder without the CommonDialog Box ...
using the SHBrowseForFolder API ...
But i was thinking is there a way to specify the selected folder upon showing the Browse for folder Dialog Box ??
I mean currently when it starts it selects My computer ...let's say i want it to be pointed to "E:\programs\Audio" by example ....
is this possible ?? it was in the Common DialogBox ...

thank you in Advance
OHDev


'Declaration in Module
Option Explicit

Public 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
Public Const BIF_RETURNONLYFSDIRS = 1
Public Const BIF_STATUSTEXT = 2
Public Const MAX_PATH = 260
Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Public Declare Function lstrcat Lib "Kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long

Public Function BrowseForFolder(HWND As Long, Optional Title As String = "") As String
    Dim iNull As Integer, lpIDList As Long, lResult As Long
    Dim sPath As String, udtBI As BrowseInfo

    With udtBI
        .hwndOwner = HWND
        .lpszTitle = lstrcat(Title, "")
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With

    lpIDList = SHBrowseForFolder(udtBI)
    If lpIDList Then
        sPath = String$(MAX_PATH, 0)
        SHGetPathFromIDList lpIDList, sPath
        CoTaskMemFree lpIDList
        iNull = InStr(sPath, vbNullChar)
        If iNull Then
            sPath = Left$(sPath, iNull - 1)
        End If
    End If

 BrowseForFolder = sPath
End Function


'calling the code:
MsgBox BrowseForFolder(Me.HWND, "Browse for folder")
LVL 7
OHDev2004Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Erick37Connect With a Mentor Commented:
"This page discusses adding the callback functionality necessary to a [VB6] application in order to to provide the ability to pre-select a folder on the dialog's display."

Pre-selecting a Folder using the Browse Callback

http://vbnet.mvps.org/index.html?code/callback/browsecallback.htm
0
 
OHDev2004Author Commented:
wow ... thank you Erick very much! ...
OHDev
0
 
OHDev2004Author Commented:
and sorry for the Low points :( .. LOL
OHDev
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.