PDA

View Full Version : [SOLVED:] VBA Macro to copy and move all files and folders from one folder to another.



staicumihai
12-27-2015, 09:07 PM
Hy everyone!

In this path:

D:\MIHAI\DOSARE\BAAR

I have this folder:
BAAR-Dosar6359_01977 CT10CXJ Dacia Logan-18.11.2015
This folder contains more files and subfolders.

I want to make a macro in Microsoft Word 2010 that does the following:


1. Copy the folder:
BAAR-Dosar6359_01977 CT10CXJ Dacia Logan-18.11.2015
(including files and subfolders)
to this path:
\\Andreea\baar\BAAR dosare\BAAR-dosare primite\BAAR-dosare in lucru

2. Move the folder:
BAAR-Dosar6359_01977 CT10CXJ Dacia Logan-18.11.2015
(including files and subfolders)
to this path:
D:\MIHAI\DOSARE\BAAR\VECHI


If you have any ideas of how to do that I would be greatly appreciative as it would save me a great amount of time at work.
Thanks a lot guys and have a good day!

gmayor
12-27-2015, 09:43 PM
I don't know why yoiu need a macro for this. It looks like a simple drag and drop using Windows Explorer.

gmaxey
12-28-2015, 07:23 AM
You can do this with the FileSystemObject:


Sub TestMoveCopy()
'Starting with a folder "H:\Test
MoveFolderFSO "H:\Test", "H:\Test2"
CopyFolderFSO "H:\Test2", "H:\Test3"
End Sub
Sub MoveFolderFSO(strSFolder, strDFolder)
Dim oFSO As Object 'FileSystemObject
'This in a sense is equivelent to renaming the source folder with the destination folder name.
Set oFSO = CreateObject("scripting.filesystemobject")
If oFSO.FolderExists(strDFolder) Then oFSO.DeleteFolder (strDFolder)
oFSO.MoveFolder strSFolder, strDFolder
lbl_Exit:
Exit Sub
End Sub
Sub CopyFolderFSO(strSFolder, strDFolder)
Dim oFSO As Object 'FileSystemObject
Set oFSO = CreateObject("scripting.filesystemobject")
oFSO.CopyFolder strSFolder, strDFolder
lbl_Exit:
Exit Sub
End Sub

staicumihai
12-30-2015, 05:12 AM
Thanks a lot gmaxey.