• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

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")
0
OHDev2004
Asked:
OHDev2004
  • 2
1 Solution
 
Erick37Commented:
"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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now