VB脚本FileSystemObject方法抛出424错误

时间:2014-07-27 01:27:47

标签: vba excel-vba vbscript excel

过去几个小时我一直在互联网上搜索,试图找出为什么这总是会在excel中抛出424 Object required错误的原因。有人可以解释为什么会这样,以及解决它的最佳方法吗?

Sub ImportXML()
'
' ImportXML Macro
'
' Keyboard Shortcut: Ctrl+k
'
Dim count As Integer
Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubFolders FSO.GetFolder("C:\Users\xxx\Desktop\yyyyy\")
End Sub

显示文件夹方法

Sub ShowSubFolders(folder)
For Each Subfolder In folder.SubFolders
getFiles Subfolder
ShowSubFolders Subfolder
Next
End Sub

获取文件方法

Sub getFiles(folder)
For Each file In folder.Files
ActiveSheet.Cells(1, 1).Value = FSO.GetFileName(file) 'throws error here
Next
End Sub

1 个答案:

答案 0 :(得分:1)

ActiveSheet.Cells(1, 1).Value = FSO.GetFileName(file)

object.GetFileName(pathspec)但你正在传递一个对象。所以

ActiveSheet.Cells(1, 1).Value = FSO.GetFileName(file.name)

或更直接

ActiveSheet.Cells(1, 1).Value = dfile.name