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.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » 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.
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.
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
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications