MSDN:Shell Object (http://msdn.microsoft.com/en-us/library/bb774094(v=vs.85).aspx)
Shell.BrowseForFolder(ByVal Hwnd As Integer, _
ByVal sTitle As BSTR, _
ByVal iOptions As Integer, _
[ ByVal vRootFolder As Variant ]) As FOLDER
Hwnd
类型:整型
父窗口的句柄,可以为 0
sTitle
类型:字符串
显示在文件夹选择对话框的字符串
iOptions
类型:整型
方法的参数选项。可以为 0 或是一组选项的组合,见 BROWSEINFO (http://msdn.microsoft.com/en-us/library/bb773205(v=vs.85).aspx)的 ulFlags 。
vRootFolder [可选]
类型:变体
对话框的根目录,用户不能访问高于该根目录。默认为 桌面 。该值可以为指定的目录路径或是一个 ShellSpecialFolderConstants (特殊目录常数,http://msdn.microsoft.com/en-us/library/bb774096(v=vs.85).aspx),VB内置了这些常数,VBA和VBS需要直接使用常数值。
示例:
Function GetFolderPath(sTitle As String) As String
Dim oShell As Object, oFolder As Object
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.BrowseForFolder(0, sTitle, 0)
If oFolder Is Nothing Then
GetFolderPath = ""
Else
GetFolderPath = oFolder.Self.Path
End If
Set oFolder = Nothing
Set oShell = Nothing
End Function
Sub search()
On Error GoTo 100
Dim wsh As Object, mypath$, ar
mypath = CreateObject("shell.application").BrowseForFolder(0, "请选择要搜索的文件夹", 0).Items.Item.Path '在此指定目录
Set wsh = CreateObject("wscript.shell")
mypath = wsh.exec("cmd /c dir /a /s /b /a-d " & Chr(34) & mypath & Chr(34)).StdOut.ReadAll
ar = Split(mypath, vbCrLf)
Set wsh = Nothing
Cells.ClearContents
Range("a1").Resize(UBound(ar) + 1) = Application.Transpose(ar)
100:
End Sub