Option Explicit
'首先引用 Microsoft Shell Controls And Automation
Private Sub fnShellBrowseForFolderVB()
Dim objShell As Shell
Dim ssfWINDOWS As Long
Dim objFolder As Folder2
Set objShell = New Shell
Set objFolder = objShell.BrowseForFolder(0, "Example", 0, SFVVO_WIN95CLASSIC)
If (Not objFolder Is Nothing) Then
MsgBox objFolder.Self.Path
End If
Set objFolder = Nothing
Set objShell = Nothing
End Sub
Private Sub Command1_Click()
fnShellBrowseForFolderVB
End Sub
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
' 双击命令按钮,写如下代码:
Private Sub Command1_Click()
Dim bi As BROWSEINFO
Dim r As Long
Dim pidl As Long
Dim path As String
Dim pos As Integer
path = Space$(512)
r = SHGetPathFromIDList(ByVal pidl&, ByVal path)
If r Then
pos = InStr(path, Chr$(0))
Text1 = Left(path, pos - 1)
Else: Text1 = ""
End If
End Sub
要获得文件的路径:
Function GetPath(FileName as string) as String
Dim Names
Dim I as long
Names = Split(FileName , "\", -1)
For I=Lbound(Names) to UBound(Names)-1
GetLastName =GetLastName & Names(I) & "\"
Next
End Function
使用时,用COMMONDIALOG随便打开一个文件,再写: