• Copy all files in folder using VBA (Word 2002)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Copy all files in folder using VBA (Word 2002)

    Author
    Topic
    #373127

    I’m having difficulty putting together VB code that will copy all files from a given folder to another folder.

    Any help will be appreciated.

    Viewing 3 reply threads
    Author
    Replies
    • #598677

      Take a look at this thread in Access. The same approach can be made to work in Word.

    • #598906

      I only have one thing to say…

      Obi-Wan FSO said…
      “use the FileSystemObject, Luke”

      • #598978

        The only thing to keep in mind about the FSO is that it’s not unheard of for IT to turn off scripting, which leaves you SOL. shrug

        • #599007

          Charlotte,

          Am open to correction but I think switching off scripting impacts on WScript and CScript (the WSH) and the .vbs (et al) files that run the scripts. The scripting runtime is more like the VB or Access runtime files.

          Andrew

          • #599037

            I don’t have first hand knowledge, only what I’ve seen posted in that regard. Apparently some IT departments go overboard. shrug

            • #599039

              As anything that can be done with the FSO can be done via VB/VBA with some API calls, there seems little point in disabling it. Some IT departments do indeed go overboard as result of ignorance and insecurity.

              Andrew

            • #599086

              During the early days of rampant .VBS deliveries in the daily e-mails, it might have made sense to take “dangerous” COM objects like the scripting runtime and Windows Script Host (WSH) out of commission for a while. Now that MS has patched many of its products, and companies have deployed additional layers of protection (from attachment blocking, to virus detection, to heuristic scanning), and desktops have been reconfigured to minimize the risk of “accidentally” executing a script (see Post #121209) it’s probably safe enough to restore those tools. But some degree of caution is appropriate; productivity cannot always win out over paranoia. Not when you’re running the world’s “most targeted” operating system.

    • #598953

      As VBnerd says, use the FSO, try the following, where pathA and pathB are the source and destination folders.

      Sub fsoCopyFiles()
      Dim fso
      Set fso = CreateObject(“Scripting.FileSystemObject”)
      fso.CopyFile “pathA*.*”, “pathB”
      Set fso = Nothing
      End Sub

      Andrew C

      • #598961

        Thanks for your help with this. The code you suggested is exactly what I was looking for. bravo

    • #598954

      Here’s an example using both CopyFile and CopyFolder

      Sub CopyWrapper()
          Dim s As String
          Dim d As String
          
          s = "C:My DocumentsFooBar"
          'And assume s has one subfolder named Foobar2
          d = "A:Test"
          '*******
          'To Copy Files ONLY
          s = s & Application.PathSeparator & "*.doc"
          'Call DoCopyFilesIn(s, d, true)
          'copies files in d into s, no subfolders copied.
          'Test now contains all the files of FooBar
          
          '*******
          'To copy a folder structure
          's = "C:My DocumentsFooBar"
          'copies last folder in s *under* d
          'A:Test now has
          'A:TestFooBar
          'A:TestFooBarFooBar2
          'Call DoCopyFolder(s, d, True)
          
          '*******
          'To copy subfolders only
          s = "C:My DocumentsFooBar*.*"
          s = "C:My DocumentsFooBar*"
          'do the same thing.
          'copies subfolders of s into d
          'A:TestFooBar2
          'Call DoCopyFolder(s, d, True)
      End Sub
      
      Sub DoCopyFilesIn(fldr As String, dest As String, over As Boolean)
      Dim fs As Object
      Dim worked As Boolean
          Set fs = CreateObject("Scripting.FileSystemObject")
          worked = fs.CopyFile(fldr, dest, over)
      End Sub
      
      Sub DoCopyFolder(fldr As String, dest As String, over As Boolean)
      Dim fs As Object
      Dim worked As Boolean
          Set fs = CreateObject("Scripting.FileSystemObject")
          worked = fs.CopyFolder(fldr, dest, over)
      End Sub
      
      
    Viewing 3 reply threads
    Reply To: Copy all files in folder using VBA (Word 2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: