Excel VBA说对象不支持此操作?
问题描述:
我有一个VBA
代码,它列出了特定文件夹中的所有文件,但它与“对象不支持此操作”错误失败。任何帮助表示赞赏。谢谢!Excel 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
答
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项目
在这行参考? –
从Excel 2007开始,文件搜索不起作用。您需要使用Dir()或其他方法:https://answers.microsoft.com/zh-CN/office/forum/office_2010-customize/applicationfilesearch-not- working/771afb4f-d948-4a37-ac8f-4366336a41fc?auth = 1 –
谢谢,Scott! – John