0
我是VBA的新手,对此程序的任何帮助都非常感谢!如何将特定文件从子文件夹复制到目标文件夹? (Excel VBA)
此程序的目标是将网络中的所有特定文件类型(.pdf)复制到桌面上的文件夹中。但是,(.pdf)文件位于每个文件夹子文件夹中。
如果我有用户定义文件夹(与许多子文件夹),我想程序将每个.pdf从每个子文件夹复制到目标文件夹。
这是我从浏览互联网到目前为止所得到的。
Sub Copy_test2()
Dim FSO As Object, fld As Object
Dim fsoFile As Object
Dim fsoFol As Object
FromPath = "D:\Users\A\Desktop\test1" 'user will define this
ToPath = "D:\Users\A\Desktop\test2" 'this will be the folder on the desktop
If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If
Set FSO = CreateObject(“Scripting.FileSystemObject”)
Set fld = FSO.GetFolder(FromPath)
If FSO.FolderExists(fld) Then
For Each fsoFol In FSO.GetFolder(FromPath).subfolders
For Each fsoFile In fsoFol.Files
If Right(fsoFile, 3) = “pdf” Then
fsoFile.Copy ToPath
End If
Next
Next
End If
End Sub
当我运行它,我得到:运行时错误“424”必选对象为
Set FSO = CreateObject(“Scripting.FileSystemObject”)
我要对这个代码的正确方法?还是有其他方法来完成这项任务?
谢谢!
谢谢你的建议!我会尝试调试这个。 – ducky