I am fairly new to VBA and any help with this program is greatly appreciated!
The goal of this program is to copy all specific file types (.pdf) from the network to a folder on the desktop. However, the (.pdf) files are in each of the folders subfolders.
If I have the user define the folder (with the many subfolders), I would like the program to copy each .pdf from each subfolder into the target folder.
This is what I have gotten so far from browsing the internet.
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
When I run it, I get : Run-time Error '424' Object Required for
Set FSO = CreateObject(“Scripting.FileSystemObject”)
Am I going about this code the right way? or is there alternative method to accomplish this task?
Thanks!
Always begin your modules with Option Explicit
. This forces you to declare all variables, which is a great thing.
Then, always compile your code before running it. This can be done from the Debug menu -> Compile. In your case, compilation will fail because a couple string variables are not declared (unless they're declared at the module level), and because you're using double quotes that aren't ", ie “ and ”. See the difference? VBA doesn't like them :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.