Function SearchForFilesInFolderTree(filename As String, filepath As String, fSearchSubFolders As Boolean) As String
Dim fso As New Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim sfld As Scripting.Folder
If fso.FileExists(filepath & filename) = True Then
SearchForFilesInFolderTree = filepath
Exit Function
End If
If fSearchSubFolders = True Then
Set fld = fso.GetFolder(filepath)
For Each sfld In fld.SubFolders
SearchForFilesInFolderTree = SearchForFilesInFolderTree(filename, sfld.Path & "\", True)
If SearchForFilesInFolderTree <> "" Then Exit Function
DoEvents
Next sfld
Else
SearchForFilesInFolderTree = ""
End If
字符串 结束函数 下面是一些关于如何调用上面代码的示例代码:
Sub TestSearch()
Dim filename As String
Dim filepath As String
Dim FoundFolder As String
Dim fSearchSubFolders As Boolean
fSearchSubFolders = True ' set to false if you only want to search the filepath folder and not subfolders
filename = "test.xlsx" ' this is the file you want to search for.
filepath = "c:\temp" ' This is the root folder of your search
If Right(Trim(filepath), 1) <> "\" Then filepath = Trim(filepath) & "\"
FoundFolder = SearchForFilesInFolderTree(filename, filepath, fSearchSubFolders)
If FoundFolder <> "" Then
MsgBox "File " & filename & " found at: " & FoundFolder
Else
MsgBox "File " & filename & " not found in: " & filepath
End If
1条答案
按热度按时间arknldoa1#
我不认为你可以得到从资源管理器返回的文件路径,而不使用一些花哨的windows代码。但是你可以用VBA模拟同样的事情。下面是一个工作代码示例。注意,它不像search=msquery那样查找部分匹配,但这是一个简单的调整。需要的话告诉我
字符串
结束函数
下面是一些关于如何调用上面代码的示例代码:
型
结束子