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
Const BIF_RETURNONLYFSDIRS = &H1
'Private pidl As Long
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
Function GetPath() As String
Dim bi As BROWSEINFO
Dim lngResult As Long
Dim pidl As Long
Dim Path As String
Dim pos As Integer
Dim strPath As String
'句柄
bi.hOwner = Me.hWnd
'展开根目录
bi.pidlRoot = 0&
'列表框标题
bi.lpszTitle = "请选择文档导出的路径:"
'规定只能选择文件夹,其他无效
bi.ulFlags = BIF_RETURNONLYFSDIRS
'调用API函数显示列表框
pidl = SHBrowseForFolder(bi)
'利用API函数获取返回的路径
Path = Space$(512)
lngResult = SHGetPathFromIDList(ByVal pidl&, ByVal Path)
If lngResult Then
pos = InStr(Path, Chr$(0))
strPath = Left(Path, pos - 1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
Else
strPath = ""
End If
GetPath = strPath
End Function
Private Sub Command1_Click()
MsgBox GetPath(Me.hWnd)
End Sub