Excel VBA Macro To Copy or Move a File from one location to another location
Sub Copy_One_File()
Dim SourceFilePath As String
Dim TargetFolderPath As String
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
SourceFile = "C:\Documents and Settings\Administrator\My Documents\SourceFolder\Sample.xlsx"
'File Path Should Specify Exactly With File Extension
TargetFolderPath = "C:\Documents and Settings\Administrator\My Documents\TargetFolder\"
If Right(TargetFolderPath, 1) <> "\" Then
TargetFolderPath = TargetFolderPath & "\"
End If
'Checking the File Existence using FSO Method
If FSO.FileExists(SourceFile) = False Then
MsgBox ("Source File Does Not Exist in Path")
End If
'Checking the File Existence using DIR Method
If Dir(SourceFile) = vbNullString Then
MsgBox ("Source File Does Not Exist in Path")
End If
On Error Resume Next
FSO.CopyFile SourceFile, TargetFolderPath
MsgBox "SuccessFull Copied"
'FSO.MoveFile SourceFilePath, TargetPath 'To Move
'MsgBox "SuccessFull Moved"
End Sub
Thanks,Tamatam
Sub Copy_One_File()
Dim SourceFilePath As String
Dim TargetFolderPath As String
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
SourceFile = "C:\Documents and Settings\Administrator\My Documents\SourceFolder\Sample.xlsx"
'File Path Should Specify Exactly With File Extension
TargetFolderPath = "C:\Documents and Settings\Administrator\My Documents\TargetFolder\"
If Right(TargetFolderPath, 1) <> "\" Then
TargetFolderPath = TargetFolderPath & "\"
End If
'Checking the File Existence using FSO Method
If FSO.FileExists(SourceFile) = False Then
MsgBox ("Source File Does Not Exist in Path")
End If
'Checking the File Existence using DIR Method
If Dir(SourceFile) = vbNullString Then
MsgBox ("Source File Does Not Exist in Path")
End If
On Error Resume Next
FSO.CopyFile SourceFile, TargetFolderPath
MsgBox "SuccessFull Copied"
'FSO.MoveFile SourceFilePath, TargetPath 'To Move
'MsgBox "SuccessFull Moved"
End Sub
Thanks,Tamatam
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.