browsing for folder. Lars-Eric

B

BRC

a month or so ago i was trying to create a form that called a "browse for
folder" routine and returned the selected folder path to a textbox.
Lar-Eric supplied me with the solution and i carelessly lost it in a hd
crash. the code he supplied used the "Shell32.Shell" variable etc.
I am working in Word 2000 and Windows 98 and Windows 2000. Can anyone steer
me to a resource for this info. I don't totally understand the shell
fuction but i was able to return the selected folder with the code. I put
is name in the subject line in the event he might see this message. thanks
for any help.
BRC
 
L

Lars-Eric Gisslén

BRC,

I don't remember if I gave you the API interface or via the Shell COM object
but here is the API version:
--------------------------------
Const BIF_RETURNONLYFSDIRS = 1
Const BIF_NEWDIALOGSTYLE = &H40
Const MAX_PATH = 260

Type BrowseInfo
hWndOwner As Long
pidlRoot As Long
pszDisplayName As Long
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Integer
End Type

Declare Function GetActiveWindow Lib "user32" () As Long
Declare Function SHBrowseForFolder Lib "shell32" _
(pBrInfo As BrowseInfo) As Long
Declare Function SHGetPathFromIDList Lib "shell32" _
(ByVal pidList As Long, _
ByVal lpBuffer As String) As Long
Declare Sub CoTaskMemFree Lib "ole32.dll" _
(ByVal pMem As Long)

Option Explicit

Public Function SelectFolder(sTitle) As String
Dim nPos As Long
Dim pidList As Long
Dim nResult As Long
Dim sPath As String
Dim pBInfo As BrowseInfo

sPath = String(MAX_PATH, Chr(0))
sTitle = sTitle & Chr(0)

With pBInfo
'Set the owner window (current active Window)
.hWndOwner = GetActiveWindow()
.lpszTitle = sTitle
' BIF_NEWDIALOGSTYLE let the user resize the Window
' and also create new folders, delete folders and some more.
' If that is not desired, use only BIF_RETURNONLYFSDIRS
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE
'.ulFlags = BIF_RETURNONLYFSDIRS
End With

pidList = SHBrowseForFolder(pBInfo)

If pidList <> 0 Then
SHGetPathFromIDList pidList, sPath
CoTaskMemFree pidList
nPos = InStr(sPath, Chr(0))
If nPos > 0 Then
sPath = Left(sPath, nPos - 1)
End If
End If

SelectFolder = sPath

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top