VBA 使用递归获取文件(对象)

VBA get file (object) using recursion

想法是检查所有子文件夹中是否存在某个文件。代码找到文件并 Debug.Print 向我打印路径,但我的函数“findFile”没有 returns 对象,只是没有。我不认为外部变量是一个好方法,所以我尽量避免这种做法。

Function getFile (ByRef outSourceFolder as Object, ByVal reportName as String) as object
Dim myFile as Object
    Set myFile =mylib.findFile(outSourceFolder, reportName) ' always returns Nothing
    If Not (myFile Is Nothing) Then
        Set getFile = myFile 
    End If
End Function

Function findFile(ByRef myFolder As Object, ByVal FileName As String) As Object
Dim iFolder, iFile As Object

    For Each iFile In myFolder.Files
        If InStr(1, iFile.Name, FileName) Then
            Set findFile = iFile
            Debug.Print iFile.Path
            Exit Function
        End If
    Next
    
' recursive call
    For Each iFolder In myFolder.SubFolders
        Call findFile(iFolder, FileName)
    Next

End Function

结果递归设置了文件对象,所以我得到了文件,但其余的调用就像覆盖了结果。

我已经解决了这个问题:

For Each iFolder In myFolder.SubFolders
    Set tFile = findFile(iFolder, FileName)
    If Not tFile Is Nothing Then
        Set findFile = tFile
        Exit Function
    End If
Next

所以我没有在需要时停止 rucursion,这是个问题。