Monday, 22 October 2012

How To Copy a File From One Path to Another Path

Excel VBA Macro To Copy  a File From One Path to Another Path

Sub Copy_All_Folder_Files()
Dim SourceFolderPath As String
Dim TargetFolderPath As String
Dim FSO As Object

SourceFolderPath = "C:\Documents and Settings\Administrator\My Documents\SourceFolder"

'File Path Should Specify Exactly

'TargetFolderPath = "C:\Documents and Settings\Administrator\My Documents"
TargetFolderPath = "F:\TargetFolder"

If Right(SourceFolderPath, 1) = "\" Then
SourceFolderPath = Left(SourceFolderPath, Len(SourceFolderPath) - 1)
End If

'If Right(TargetFolderPath, 1) = "\" Then
'TargetFolderPath = Left(TargetFolderPath, Len(TargetFolderPath) - 1)
'End If

Set FSO = CreateObject("Scripting.FileSystemObject")

If FSO.FolderExists(SourceFolderPath) = False Then
MsgBox ("Source Folder Does Not Exist or Path Not Found")
End If

If FSO.FolderExists(TargetFolderPath) = False Then
MsgBox ("Target Folder Does Not Exist or Path Not Found")
End If

On Error Resume Next

FSO.CopyFolder SourceFolderPath, TargetFolderPath
MsgBox "SuccessFull Copied"

End Sub

No comments:

Post a Comment

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts