'我又改了改:
'也能指定 RootFolder (默认目录),能得到"文件夹路径"和"绝对路径"
'引用 Microsoft Shell Controls And Automation
Dim x As New Shell32.Shell
Dim y As Shell32.Folder
'Set y = x.BrowseForFolder(Me.hWnd, "Select Folder:", 1, "c:\")
Set y = x.BrowseForFolder(Me.hWnd, "Select Folder:", 1)
If Not y Is Nothing Then
Dim sFoldersPath As String
Dim sPath As String
sPath = y
sFoldersPath = y
Do Until y.ParentFolder Is Nothing
sFoldersPath = y.ParentFolder & "\" & sFoldersPath
If VBA.InStr(sPath, ":") = 0 Then
If Not y.ParentFolder Like "*:*" Then
sPath = y.ParentFolder & "\" & sPath
Else
sPath = VBA.Mid(y.ParentFolder, VBA.InStr(y.ParentFolder, ":") - 1, 2) & "\" & sPath
'Exit Do
End If
End If
Set y = y.ParentFolder
Loop
If VBA.Len(VBA.Trim(sPath)) > 0 Then
MsgBox "Path: " & sPath & vbCrLf & "Folder Path: " & sFoldersPath
End If
End If
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Private Function GetDir(ByVal szCaption As String) As String
Dim bi As BROWSEINFO, idl As ITEMIDLIST, rtn As Long, pidl As Long, pos As Long, path As String
bi.hOwner = Me.hwnd
bi.lpszTitle = szCaption
bi.ulFlags = BIF_RETURNONLYFSDIRS
pidl = SHBrowseForFolder(bi)
path = Space(512)
rtn = SHGetPathFromIDList(ByVal pidl, path)
If rtn Then pos = InStr(path$, Chr$(0)): GetDir = Left(path, pos - 1)
End Function
Private Sub Command1_Click()
'New:
'引用 Microsoft Shell Controls And Automation
Dim x As New Shell32.Shell
Dim y As Shell32.Folder
Set y = x.BrowseForFolder(Me.hWnd, "Select Folder:", 1)
Dim sFoldersPath As String
If Not y Is Nothing Then
Dim sPath As String
sPath = y
sFoldersPath = y
Do Until y.ParentFolder Is Nothing
sFoldersPath = y.ParentFolder & "\" & sFoldersPath
If Not y.ParentFolder Like "*:*" Then
sPath = y.ParentFolder & "\" & sPath
Else
sPath = VBA.Mid(y.ParentFolder, VBA.InStr(y.ParentFolder, ":") - 1, 2) & "\" & sPath
'Exit Do
End If
Set y = y.ParentFolder
Loop
If VBA.Len(VBA.Trim(sPath)) > 0 Then
MsgBox "Path: " & sPath & vbCrLf & "Folder Path: " & sFoldersPath
End If
End If
Public Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
'可见只有一个参数BROWSEINFO,这是一个类型,定义如下:
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
'SHGetPathFromIDList()API的申明如下:
Public Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
'另外还要申明一些常量,用于ulFlags的设置:
Public Const BIF_RETURNONLYFSDIRS = &H1 '<---我用的是这个,显示所有磁盘……
Public Const BIF_DONTGOBELOWDOMAIN = &H2
Public Const BIF_STATUSTEXT = &H4
Public Const BIF_RETURNFSANCESTORS = &H8
Public Const BIF_BROWSEFORCOMPUTER = &H1000
Public Const BIF_BROWSEFORPRINTER = &H2000
SHBrowseForFolder()的申明如下:
Public Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
可见只有一个参数BROWSEINFO,这是一个类型,定义如下:
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
SHGetPathFromIDList()API的申明如下:
Public Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
另外还要申明一些常量,用于ulFlags的设置:
Public Const BIF_RETURNONLYFSDIRS = &H1 <---我用的是这个,显示所有磁盘……
Public Const BIF_DONTGOBELOWDOMAIN = &H2
Public Const BIF_STATUSTEXT = &H4
Public Const BIF_RETURNFSANCESTORS = &H8
Public Const BIF_BROWSEFORCOMPUTER = &H1000
Public Const BIF_BROWSEFORPRINTER = &H2000
程序如下:
Private Sub cmdLocation_Click()
Dim LocDir As BROWSEINFO
Dim RetVal, PidLoc As Long
Dim Path As String
Dim Pos As Integer
'New:
'引用 Microsoft Shell Controls And Automation
Dim x As New Shell32.Shell
'MsgBox x.BrowseForFolder(Me.hWnd, "Select Folder:", 1)
x.BrowseForFolder(Me.hWnd, "Select Folder:", 1)