這個想法是檢查所有子檔案夾是否存在某個檔案。代碼找到檔案,Debug.Print 列印出路徑,但我的函式“findFile”不回傳物件,只是什么都沒有。我不認為 external var 是一個好方法,所以我盡量避免這種做法。
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
結果遞回設定了檔案物件,所以我得到了檔案,但其余的呼叫就像覆寫了結果。
uj5u.com熱心網友回復:
我已經解決了這個問題:
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,這是一個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/482930.html
