我有一个VBA
代码,列出了特定文件夹中的所有文件,但是它失败并显示“对象不支持此操作”错误。任何帮助表示赞赏。谢谢!
Sub ListAllFiles()
Dim fs As FileSearch, ws As Worksheet, i As Long
Set fs = Application.FileSearch
With fs
.SearchSubFolders = True
.FileType = msoFileTypeAllFiles
.LookIn = "C:Files"
If .Execute > 0 Then
Set ws = Worksheets.Add
For i = 1 To .FoundFiles.Count
ws.Cells(i, 1) = .FoundFiles(i)
Next
Else
MsgBox "No files found"
End If
End With
End Sub
答案 0 :(得分:0)
使用FileSystemObject :
Sub ListAllFiles()
Dim FSO As New Scripting.FileSystemObject
Dim foundFile As File
For Each foundFile In FSO.GetFolder(ThisWorkbook.Path).Files
Debug.Print foundFile.Name
Next
End Sub
您必须将Microsoft Scripting Runtime
的引用添加到您的VBA项目