Results 1 to 11 of 11
  1. #1
    New Lounger
    Join Date
    Jun 2002
    Location
    San Leandro, California, USA
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Copy all files in folder using VBA (Word 2002)

    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.

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Copy all files in folder using VBA (Word 2002)

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

  3. #3
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy all files in folder using VBA (Word 2002)

    I only have one thing to say...

    Obi-Wan FSO said...
    "use the FileSystemObject, Luke"

  4. #4
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Copy all files in folder using VBA (Word 2002)

    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

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy all files in folder using VBA (Word 2002)

    Here's an example using both CopyFile and CopyFolder

    <pre>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

    </pre>


  6. #6
    New Lounger
    Join Date
    Jun 2002
    Location
    San Leandro, California, USA
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy all files in folder using VBA (Word 2002)

    Thanks for your help with this. The code you suggested is exactly what I was looking for. <img src=/S/bravo.gif border=0 alt=bravo width=16 height=30>

  7. #7
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Copy all files in folder using VBA (Word 2002)

    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. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  8. #8
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Copy all files in folder using VBA (Word 2002)

    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

  9. #9
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Copy all files in folder using VBA (Word 2002)

    I don't have first hand knowledge, only what I've seen posted in that regard. Apparently some IT departments go overboard. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  10. #10
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Copy all files in folder using VBA (Word 2002)

    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

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Copy all files in folder using VBA (Word 2002)

    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=Post #121209,121209>Post #121209<!/post>) 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •