Hello -
Anyone able to assist a newbie on VBA coding, please.
I have learned some great stuff reading comments and watching online videos, etc.
But, I have patched together some code from various excel experts to:
1. save the file and then email as an excel attachment (.xlsx)
The issue currently running into thus far, is if i have already saved the file, then re-open to make adjustments, and then click my macro button to finally email the file. I get a run time error 1004: You cannot save this workbook with the same name as another open workbook or add-in. Choose a different name, or close the other workbook or add-in before closing.
The Debug checker points to this line of code as error - ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
Is there away to skip or ignore the run time error and still have excel re-save or overwrite the existing with same file name?
Here is my full code:
Anyone able to assist a newbie on VBA coding, please.
I have learned some great stuff reading comments and watching online videos, etc.
But, I have patched together some code from various excel experts to:
1. save the file and then email as an excel attachment (.xlsx)
The issue currently running into thus far, is if i have already saved the file, then re-open to make adjustments, and then click my macro button to finally email the file. I get a run time error 1004: You cannot save this workbook with the same name as another open workbook or add-in. Choose a different name, or close the other workbook or add-in before closing.
The Debug checker points to this line of code as error - ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
Is there away to skip or ignore the run time error and still have excel re-save or overwrite the existing with same file name?
Here is my full code:
Rich (BB code):
Sub Mail_workbook_Outlook_2()
'Working in Excel 2000-2016
'Mail a copy of the ActiveWorkbook with another file name
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim wb1 As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
Dim NewFN As Variant
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb1 = ActiveWorkbook
'Copy req to a new workbook
Sheets.Copy
NewFN = "C:\Users\tbomar\Documents\Requisitions\Submitted Reqs\Material Req" & Range("H4").Value & Range("H2").Value & Range("B3").Value & ".xlsx"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
ActiveWorkbook.Close
'Make a copy of the file/Open it/Mail it
'If you want to change the file name then change only TempFileName
TempFilePath = Environ$("temp") & ""
TempFileName = "Material Req." & Range("H4").Value & Range("H2").Value & Range("B3").Value & ".xlsx"
FileExtStr = ".xlsx" & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Range("H4").Value = Left(Range("H4").Value, 3) & Mid(Range("H4").Value, 4, 4) + 1
Range("A7:A31").ClearContents
Range("A33,A36").ClearContents
Range("D7:H30").ClearContents
Range("H31").ClearContents
Range("I7:P30").ClearContents
Range("E3").ClearContents
Range("H2").ClearContents
ActiveWorkbook.Save
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "reqs@gandhtowing.com"
.CC = ""
.BCC = ""
.Subject = ""
.Body = "Please see attached material requisition"
.Attachments.Add TempFilePath & TempFileName & FileExtStr
.Display 'or use .send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Last edited by a moderator: