Skip to main content

Send email from excel

Hi All,

As we all play around a lot with Excel in our Day to Day tasks, so automating that is a must thing.

Below is a code to send the whole excel sheet via email on the click of a button, I know you can find this on various other sites as well, but I am also sharing the same as well.

Below is the Macro to send a excel file as a attachment.


Sub Send_email()


Dim wb As Workbook
Dim I As Long
Set wb = ActiveWorkbook


    If Val(Application.Version) >= 12 Then
        If wb.FileFormat = 51 And wb.HasVBProject = True Then
            MsgBox "There is VBA code in this xlsx file, there will be no VBA code in the file you send." & vbNewLine & _
                   "Save the file first as xlsm and then try the macro again.", vbInformation
            Exit Sub
        End If
    End If


    On Error Resume Next   
    
   wb.sendmail "xxx@abc.com", _
                    "Please find the attached email"
                    MsgBox "Mail Sent Successfully "                   
                  


    On Error GoTo 0
End Sub


The Code is really small but the utility is Big.

Also if you want to send a email on click of a button but also needs to send a copy to another person which is optional, so you can make a new userform in excel and place the checkbox and button and can then place the event over there like

Private Sub CommandButton1_Click()

Dim wb As Workbook
Dim I As Long
Set wb = ActiveWorkbook

    If Val(Application.Version) >= 12 Then
        If wb.FileFormat = 51 And wb.HasVBProject = True Then
            MsgBox "There is VBA code in this xlsx file, there will be no VBA code in the file you send." & vbNewLine & _
                   "Save the file first as xlsm and then try the macro again.", vbInformation
            Exit Sub
        End If
    End If

    On Error Resume Next    
    If CheckBox1.Value = True Then
    
   wb.sendmail "xxx@xx.com", _
                    "Please find the attachedsheet"
                    MsgBox "Mail Sent Successfully!!"
                    
                    wb.sendmail " xxx@abc.com", _
                    " Please find the attached sheet "
                    MsgBox " A copy has been sent successfully!! "
                    
           Me.Hide
           Else
           wb.sendmail "xxx@xx.com", _
                    "Please find the attached sheet"
                    MsgBox "Mail Sent Successfully!!"
                    Me.Hide
                    
                    End If    
    On Error GoTo 0
End Sub

Place the above code in the userform1 commandbutton click event and simply call this form via macro in Modules like given below...

Module 1

Sub Send_mail_Userform()
Userform1.show

End Sub


And assign the macro Send_mail_Userform() to a object, button or whatever you like and you are DONE !!!


Let me know if any one has requirement for send a email as single worksheet, selected area or multiple tabs from excel.

Cheers!!

Happy Blogging...

Comments

Popular posts from this blog

Install SugarCRM - Step by Step Guide

SugarCRM is the market-leading, commercial Open Source customer relationship management (CRM) application. SugarCRM’s Open Source architecture easily adapts to any business environment by offering a more flexible, cost-effective alternative than proprietary applications. It offers a complete CRM system for businesses of all sizes. For a small business SugarCRM can be a huge time and expense saver. It requires minimal technical knowledge to get it installed and the built in web administration console and backup system allow you to feel comfortable that it’s going to keep running. Core SugarCRM functionality includes sales automation, marketing campaigns, support cases, project mgmt, calendaring and more. Built in PHP, supports MySQL and SQL Server allows it that cool functionality LAMP system. We have tested it Windows XP/Vista/2k3 . Step 1: Installing XAMPP on Windows 1. Go to http://www.apachefriends.org/en/xampp-windows.html#641 and click "XAMPP Windows 1.7.1". Download...

STRESS MANAGEMENT

STRESS Stress is an unpleasant fact of life.  We all experience it for various reasons,  and we all try to come up with ways of  coping with it—some with more success  than others. So what exactly is stress  doing to your mind (and body)  when you're staring down a deadline?  And what can you do to power through it? What Stress Actually Does  to You and What You Can  Do About It The real problem with stress is that, for such a well  understood and universally experienced condition,  as a society we deal with it so poorly that it leads  to many of our most lethal illnesses and  long-term health problems. High blood  pressure, heart disease, cancer, stroke,  obesity, and insomnia are all medical  conditions across the spectrum that  can be related to or directly influenced  by high stress as an environmental conditio...

Jquery Carousels

Jquery Carousels we all love carousels they are a fantastic way to give the effects we want our visitors to have, more than that we have multiple components to show like images, links, text etc. There are lot many ways to achieve it, Jquery is ofcourse the best possible option available outside. We all search for lot of ems it can be Jquery Flexslider  or Jcarousel Lite  whatever you choose, customization is required, in this tutorial I am not going to focus on how to install these libraries rather one step ahead, to let you know how these carousels can be called multiple times in the same page having their controls working respectively for each carousel instance. So, lets get started with the HTML <div id="sideRight"> <div id="first">       <!-- Do not change the class and tag type, as this will remain as it is for all the following divisions-->       <p class="containheader">Plans for you <a ...