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