這是從我的數據庫中的東西。我一直在使用它已經有一段時間了,現在是VBA。此代碼是而不是,我很久以前在網絡上發現它。
Sub Sample()
ret = BrowseForFolder("C:\")
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
Dim ShellApp As Object
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
Set ShellApp = Nothing
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
BrowseForFolder = False
End Function
在我看來,你已經在做的是最好的方法。一個(複雜的)替代方案是使用SHBrowseForFolder API函數,這是我很久以前使用的:http://allapi.mentalis.org/apilist/SHBrowseForFolder.shtml – markblandford 2012-02-22 17:24:52
+1好問題 – 2012-02-22 18:40:56