Bonjour,
Dans son message, < Carl > écrivait :
In this message, < Carl > wrote:
|| Hi all
|| What is the code to save a path to a string using a
|| browsing dialog box.
|| I know how to use the open dialog box but I don't want to
|| open/run anything, just save the directory path as a
|| varible.
Try this. Call tnhbe procedure using the "Folder_API" sub.
'_______________________________________
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
'sTitle = text inside browse for folder box
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
'_______________________________________
'_______________________________________
Sub Folder_API()
Dim Mypath As String
Mypath = SelectFolder("Choose a folder")
If Replace(Mypath, Chr(0), "") = "" Then
MsgBox "No folder was chosen, or action was cancelled.", _
vbExclamation, "No folder"
Exit Sub
End If
MsgBox Mypath
End Sub
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org