早期绑定(需要对Microsoft脚本运行时的引用):
Public Sub EnumerateDirectory() Dim fso As Scripting.FileSystemObject Set fso = New Scripting.FileSystemObject Dim targetFolder As Folder Set targetFolder = fso.GetFolder("C:\") Dim foundFile As Variant For Each foundFile In targetFolder.Files Debug.PrintfoundFile.Name Next End Sub
后期绑定:
Public Sub EnumerateDirectory() Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") Dim targetFolder As Object Set targetFolder = fso.GetFolder("C:\") Dim foundFile As Variant For Each foundFile In targetFolder.Files Debug.PrintfoundFile.Name Next End Sub