0
我想傳遞一個VBA模塊中的文件夾中對象的集合,但它一貫執行的對象爲String類型如何防止MS Access VBA將對象轉換爲字符串?
Function GetFolderFiles(ByRef dir As Object) As Collection
Dim files As Collection
Set files = New Collection
For Each file In dir.files
Debug.Print TypeName(file)
files.Add (file)
Next frile
For Each subfolder In dir.SubFolders
Dim sf As Object
Set sf = subfolder
Debug.Print TypeName(sf)
Dim tmpfiles As Collection
Set tmpfiles = GetFolderFiles(sf)
For Each File In tmpfiles
files.Add (File)
Next File
Next subfolder
Set GetFolderFiles = files
End Function
這裏隱式轉換是我的測試代碼:
Function TestGetFiles()
Dim fso As Object
Dim d As Object
Dim files As Collection
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetFolder("C:\Users\X\Documents\My Web Sites")
Set files = GetFolderFiles(d)
For Each f In files
Debug.Print TypeName(f)
Next f
End Function
正如你所看到的,我打印出TypeName()調用的結果。當我遍歷這些函數時,我在GetFolderFiles中獲得預期的類型「File」,但是當我迭代返回的集合時,對象的類型是「String」。
1)如何從我的函數中獲取File對象的集合?
2)更一般地說,如何防止對象隱式轉換爲字符串?
謝謝蒂姆,這解決了它。我感謝幫助! – egalitarianeric