Solved

Browse for Folder using API "SHBrowseForFolder"

Posted on 2004-10-04
3
415 Views
Last Modified: 2008-02-01
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
Comment
Question by:OHDev2004
  • 2
3 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 50 total points
ID: 12218934
"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
 
LVL 7

Author Comment

by:OHDev2004
ID: 12218977
wow ... thank you Erick very much! ...
OHDev
0
 
LVL 7

Author Comment

by:OHDev2004
ID: 12218995
and sorry for the Low points :( .. LOL
OHDev
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

803 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