In previous Article, i had written how to Send Email from Excel Macro. In that article we had discussed how we can send any random file as an Attachment.
Here in this article, you will learn how to send the ActiveWorkbook as an attachment. There could be two ways of sending the same workbook as an attachment –
1) Send the Last saved version of the workbook as an attachment.
2) First Save as the ActiveWorkbook at a temporary location with a given name and then attach it to mail and send it. After sending the email, delete the temporary file saved.
1) 1st Method:
In this method you can simply attach the Last saved version of the workbook as an attachment and mail will be sent. But if you want a different name and some modification in the workbook (WHICH HAS TO BE SENT) then follow the 2nd Method given below.
Sub Email_CurrentWorkBook()
'Do not forget to change the email ID
'before running this code
Dim OlApp As Object
Dim NewMail As Object
Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)
On Error Resume Next
With NewMail
.To = "info@learnexcelmacro.com"
.CC = "info@learnexcelmacro.com"
.BCC = "info@learnexcelmacro.com"
.Subject = "Type your Subject here"
.Body = "Type the Body of your mail"
.Attachments.Add ActiveWorkbook.FullName
.Send 'or use .Display to show you the email before sending it.
End With
On Error GoTo 0
Set NewMail = Nothing
Set OlApp = Nothing
End Sub
1) 2nd Method:
In this Method, we are following the following method:
Step 1. First Save the ActiveWorkbook at a temporary location with a given name. You can modify details if you wish, in this copy, because this is the copy we are going to send in email as an attachment. By doing so, your original workbook will remain unchanged.
Step 2. Attach this Temporary file in the email as an attachment and send email.
Step 3. Now delete this file from the temporary location.
Sub Email_CurrentWorkBook()
'Do not forget to change the email ID
'before running this code
Dim OlApp As Object
Dim NewMail As Object
Dim TempFilePath As String
Dim FileExt As String
Dim TempFileName As String
Dim FileFullPath As String
Dim MyWb As Workbook
Set MyWb = ThisWorkbook
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Save your workbook in your temp folder of your system
'below code gets the full path of the temporary folder
'in your system
TempFilePath = Environ$("temp") & "\"
'Now get the extension of the file
'below line will return the extension
'of the file
FileExt = "." & LCase(Right(MyWb.Name, Len(MyWb.Name) - InStrRev(MyWb.Name, ".", , 1)))
'Now append a date and time stamp
'in your new file
TempFileName = MyWb.Name & "-" & Format(Now, "dd-mmm-yy h-mm-ss")
'Complete path of the file where it is saved
FileFullPath = TempFilePath & TempFileName & FileExt
'Now save your currect workbook at the above path
MyWb.SaveCopyAs FileFullPath
'Now open a new mail
Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)
On Error Resume Next
With NewMail
.To = "info@learnexcelmacro.com"
.CC = "info@learnexcelmacro.com"
.BCC = "info@learnexcelmacro.com"
.Subject = "Type your Subject here"
.Body = "Type the Body of your mail"
.Attachments.Add FileFullPath '--- full path of the temp file where it is saved
.Send 'or use .Display to show you the email before sending it.
End With
On Error GoTo 0
'Since mail has been sent with the attachment
'Now delete the temp file from the temp folder
Kill FileFullPath
'set nothing to the objects created
Set NewMail = Nothing
Set OlApp = Nothing
'Now set the application properties back to true
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Works brilliantly. Thank you.
Dear Vish Bhaia,
Today I have down loaded your tools and it works perfectly.
Thanks for nice tools. I will send “Eid Greeting” to my friend
by using your tools.
[I have pressed send button without changing info@learnexcelmacro.com
So automatically you got 6 fake email. I wasn’t aware. extremely sorry for this ]
Hi Vish
Thanks a lot it works fine. One small clarification can i include a specific table from sheet one to the body of the mail is it possible
Hi Vishwa,
The 1st coding is working fine. Thanks very much..
Regards,
Ramachandran
Hi Vishwamitra,
I tried the 1st code, but it does not attach the activeworkbook in outlook. Please let me know if i am missing anything here.
.Attachments.Add ActiveWorkbook.FullName
Hi Vineeta,
I do not see any problem with this statement. Can you please share the error you get after executing this statement?
This works great. Is it possible to have the user save the file and assign a name instead of it being saved in a temporary folder?
This works great.
How can i name the subject line dynamically with the name of the attachment (as the attachment as time stamp) ?
Please advise
Nevermind- I found solution.
Dim SubjectLine As String
SubjectLine = “Desired Static Subject line ” & Format(Now, “mm-dd-yyyy”)
‘the above will append the current time stamp
‘and called the variable in the code as below
With NewMail
.To = “Email address”
.CC = “Email address”
.BCC = “Email Address”
.Subject = SubjectLine
.Body = “Static body of the email”
.Attachments.Add FileFullPath
.Display
Hi Vishwamitra Mishra,
How do I make the file extension .pdf with the code above? Using the second method.
Thanks
Nick
Awesome. Thanks.
Hello
I would like to use your macro at work.
Is that OK?
Who do I credit ?
Hi Vishwamitra, Thanks so much for the code, I’m such a beginner!
I was wondering how I can send the workbook as a Non-macro-enabled workbook? (.xlsx). I tried replacing the extension information with simply “xlsx”, and then using the fileformat property in a few places but it didn’t work.
Hi Vishwamitra,
Thanks for the code! Issue i am running into with the second option – If i leave the workbook as .display, i am getting a Run-time error ’53’: File not found for the Kill FileFullPath. After further modifications to the message and sending it out, the file is still in my temp folder. Any idea how to help remove the error & temp file so that i can still edit it? I tried .Send to myself to test, and it gave me the same error. Any help would be appreciated. Thanks!
Hi, 2nd method worked great. Thank you
You are welcome!