dougmarkham
Active Member
- Joined
- Jul 19, 2016
- Messages
- 252
- Office Version
- 365
- Platform
- Windows
Hi Folks,
Basic goal:
To move a folder and its contents into another folder that already exists.
Detail:
I wish to use VBA to move a single folder called 13) Customer Template (and all its contents including folders) from its current folder-path S:\Projects (5)\13) Customer Template\ to a new location S:\COMPLETED\ i.e., so the new folder-path is S:\COMPLETED\13) Customer Template\.
To do this, I wish to refer to the ToPath and FromPath via reference to the ActiveCell (red text below) i.e., if the ToPath is in the ActiveCell, the VB code will refer to the FromPath using Offset from the ActiveCell (the FromPath is in the same row, one column to the left).
I have the following code below:
When I run this code, I get the MsgBox: "S:\COMPLETED\" exists, not possible to move to an existing folder".
When I use the intended folderpath---S:\COMPLETED\13) Customer Template\ --- as the ToPath (in the ActiveCell), I get the error message: Run-time error '5': Invalid procedure call or argument.
Although I've moved files before, I'm not familiar with moving folders and their contents.
Would you please help me trouble-shoot this code?
Kind regards,
Doug.
P.S. in References, tools: Microsoft Scripting Runtime is ticked.
Basic goal:
To move a folder and its contents into another folder that already exists.
Detail:
I wish to use VBA to move a single folder called 13) Customer Template (and all its contents including folders) from its current folder-path S:\Projects (5)\13) Customer Template\ to a new location S:\COMPLETED\ i.e., so the new folder-path is S:\COMPLETED\13) Customer Template\.
To do this, I wish to refer to the ToPath and FromPath via reference to the ActiveCell (red text below) i.e., if the ToPath is in the ActiveCell, the VB code will refer to the FromPath using Offset from the ActiveCell (the FromPath is in the same row, one column to the left).
FromPath | ToPath |
S:\Projects (5)\13) Customer Template\ | S:\COMPLETED\ |
I have the following code below:
VBA Code:
Sub Move_Rename_Folder()
'This example move the folder from FromPath to ToPath.
Dim fso As Object
Dim CurrentFrom As Range
Dim CurrentTo As Range
Dim FromPath As String
Dim ToPath As String
Set CurrentTo = Application.ActiveCell
Set CurrentFrom = Application.ActiveCell.Offset(0, -1)
FromPath = CurrentFrom.Value
ToPath = CurrentTo.Value
'Note: It is not possible to use a folder that exist in ToPath
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set fso = CreateObject("scripting.filesystemobject")
If fso.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
If fso.FolderExists(ToPath) = True Then
MsgBox ToPath & " exists, not possible to move to an existing folder"
Exit Sub
End If
fso.MoveFolder Source:=FromPath, Destination:=ToPath
MsgBox "The folder is moved from " & FromPath & " to " & ToPath
End Sub
When I run this code, I get the MsgBox: "S:\COMPLETED\" exists, not possible to move to an existing folder".
When I use the intended folderpath---S:\COMPLETED\13) Customer Template\ --- as the ToPath (in the ActiveCell), I get the error message: Run-time error '5': Invalid procedure call or argument.
Although I've moved files before, I'm not familiar with moving folders and their contents.
Would you please help me trouble-shoot this code?
Kind regards,
Doug.
P.S. in References, tools: Microsoft Scripting Runtime is ticked.