How to Send ActiveWorkbook as attachment in Email

.

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.

Send workbook as attachment

Send workbook as 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

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

How to Copy content from Word using VBA

As many of us want to deal with Microsoft Word Document from Excel Macro/VBA. I am going to write few articles about Word from Excel Macro. This is the first article which opens a Word Document and read the whole content of that Word Document and put it in the Active...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

16 Comments

  1. Richard

    Works brilliantly. Thank you.

    Reply
  2. Nur

    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 ]

    Reply
  3. shyam

    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

    Reply
  4. Ramachandran

    Hi Vishwa,

    The 1st coding is working fine. Thanks very much..

    Regards,

    Ramachandran

    Reply
  5. Vineeta

    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

    Reply
    • Vishwamitra Mishra

      Hi Vineeta,

      I do not see any problem with this statement. Can you please share the error you get after executing this statement?

      Reply
  6. Evon

    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?

    Reply
  7. Raj

    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

    Reply
  8. Raj

    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

    Reply
  9. Nick

    Hi Vishwamitra Mishra,

    How do I make the file extension .pdf with the code above? Using the second method.

    Thanks

    Nick

    Reply
  10. Nicolas

    Awesome. Thanks.

    Reply
  11. Dania

    Hello
    I would like to use your macro at work.
    Is that OK?
    Who do I credit ?

    Reply
  12. liz

    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.

    Reply
  13. Ryan

    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!

    Reply
  14. Mike

    Hi, 2nd method worked great. Thank you

    Reply
    • Vishwamitra Mishra

      You are welcome!

      Reply

Trackbacks/Pingbacks

  1. Learn Excel Macro How to Send ActiveSheet as Attachment in mail - [...] previous article of Send Email Tutorial using Excel Macro, you learnt how to send current workbook as attachment in…
  2. How to Send ActiveSheet as Attachment in mail - Welcome to LearnExcelMacro.com - […] previous article of Send Email Tutorial using Excel Macro, you learnt how to send current workbook as attachment in…
  3. Methods to save a Macro FREE copy of a workbook [FREE DOWNLOAD] - Let's excel in Excel - […] You have an excel workbook which has Excel VBA code inside it to generate a report or to perform…

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest