0

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!

Community
  • 1
  • 1
ducky
  • 1
  • 1

1 Answers1

0

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 ", i.e. “ and ”. See the difference? VBA doesn't like them :-)

Excelosaurus
  • 2,789
  • 1
  • 14
  • 20