The idea is to check all subfolders if certain file is exist in anyone of them. The code finds the file and Debug.Print prints me the Path, but my function "findFile" does not returns the object, just Nothing. I do not think that external var is a good way, so I try to avoid this practice.
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
A resultative recurcion sets the file object, so i get the file, but the rest of calls are like overwites the result.
CodePudding user response:
I've fixed the problem with this:
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
So I didn't stop the rucursion when needed and that was a problem.