Sub copyFile()
Dim sourceFile As String
Dim DestFile As String
Dim msg As String
sourceFile = "D:\Test\Test.xls"
DestFile = "D:\Test\Test\Test2.xls"
'// Check if source file doesn't exist
If Dir(sourceFile) = "" Then
msg = "Source not found" & vbCr & " • " & sourceFile
End If
If Dir(DestFile) <> "" Then
msg = msg & vbCr & "Destination file already Exists" & vbCr & " • " & DestFile
End If
If Len(msg) = 0 Then
FileCopy sourceFile, DestFile
Else
MsgBox msg
End If
End Sub
Sub CopyByOpening()
Dim bgExcel As New Excel.Application
Dim wb As Workbook
Dim sourceFile As String
Dim DestFile As String
bgExcel.Visible = False
sourceFile = "D:\Test\Test.xls"
DestFile = "D:\Test\Test\Test3.xls"
Set wb = bgExcel.Workbooks.Add(sourceFile)
wb.SaveCopyAs DestFile
wb.Close SaveChanges:=False
bgExcel.Quit
End Sub
Sub copyActiveWorkBook()
Dim wb As Workbook
Dim ActiveWB As Workbook
Dim srcFile As String
Dim DestFile As String
Dim NameSuffix As String
Dim overWriteResponse As Integer
Dim extPos As Integer
Set ActiveWB = ActiveWorkbook
NameSuffix = ActiveWB.Sheets("SET UP").Range("Q2")
srcFile = ActiveWB.FullName
Dim bgExcel As New Excel.Application
bgExcel.Visible = False
extPos = InStrRev(srcFile, ".")
'// Insert the contents of Q2 at the end of existing file name
DestFile = Left(srcFile, extPos - 1) & NameSuffix & Mid(srcFile, extPos)
'Look to see if file already exists, don't want to over-write it
If Dir(DestFile) <> "" Then
overWriteResponse = MsgBox(" The File " + DestFile + " Already Exists", vbYesNo, "Do you want to replace the file ?")
'\\ Simplified the test exiting sub if the file is not to be overwritten
If overWriteResponse <> vbYes Then Exit Sub
End If
Set wb = bgExcel.Workbooks.Add(srcFile)
wb.SaveCopyAs DestFile
wb.Close SaveChanges:=False
bgExcel.Quit
MsgBox "Copied the file"
End Sub