我有几个宏,我知道它们会覆盖现有文件-我需要重写此宏,以便它不会覆盖文件。我尝试了各种不同的解决方案,但似乎无法让它们在我的框架内工作。
这是我到目前为止编写的宏:
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
子SaveFileExcel() 暗淡的路径作为字符串 昏暗的filename1作为字符串路径=“ U:\ Public \ WAKKA-WAKKAWAKKA” filename1 = Range(“ W1”)。Text Application.DisplayAlerts = True'如果Dir(“ f:ull \ path \ with \ filename.xls”)<>“”然后 'Kill“ f:ull \ path \ with \ filename.xls”'如果ActiveWorkbook.SaveAs结尾
文件名:= path&filename1&“ .xlsm”,文件格式:= xlOpenXMLWorkbookMacroEnabled Application.DisplayAlerts = True End Sub
答案 0 :(得分:1)
如果文件已经存在,您需要确定新名称是什么...向文件名添加时间戳通常有助于保持文件的唯一性。
只需重用您的代码:
Sub SaveFileExcel()
Dim path As String
Dim filename1 As String
path = "U:\Public\WAKKA - WAKKAWAKKA"
filename1 = Range("W1").Text
Application.DisplayAlerts = True
If Not Dir(path & filename1 & ".xlsm") <> "" Then
filename1 = filename1 & "file_already_exists_with_same_name"
End If
ActiveWorkbook.SaveAs Filename:=path & filename1 & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
Application.DisplayAlerts = True
End Sub