I need to rewrite this macro so it won't overwrite the file. I tried various solutions, but I can't get them to work.
Here is the macro I have written so far:
Sub email_workbook()
Dim wb1 As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb1 = ActiveWorkbook
TempFilePath = Environ$("temp") & "\"
TempFileName = Range("H22") & wb1.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "person1@PLACE.COM"
.CC = "MPERSON@PLACE.COM" & " " & "LPERSON@PLACE.COM"
.BCC = ""
.Subject = "SUBJECT" & Range("H22")
.Body = "Please review ETC.ETC."
.Attachments.Add TempFilePath & TempFileName & FileExtStr
.Display
End With
On Error GoTo 0
Dim myFile As String
myFile = ActiveWorkbook.Name
Application.DisplayAlerts = False ' Disregard overwriting message.
ActiveWorkbook.SaveAs Filename:="U:\Public\WAKKA\WAKKAWAKKA - To Review"
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Call SaveFileExcel
End Sub
Sub SaveFileExcel()
Dim path As String
Dim filename1 As String
path = "U:\Public\WAKKA - WAKKAWAKKA"
filename1 = Range("W1").Text
Application.DisplayAlerts = True
'If Dir("f:ull\path\with\filename.xls") <> "" Then
' Kill "f:ull\path\with\filename.xls"
'End If ActiveWorkbook.SaveAs
Filename:=path & filename1 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Application.DisplayAlerts = True
End Sub
The format with which the file name is created is important for downstream usage, so a timestamp (with actual time) isn't an option.
How can I add a "-2" or "-3", etc. to the end of the file name?