Note: The other languages of the website are Google-translated. Back to English
English English

How to send email if button is clicked in Excel?

Supposing you need to send email through Outlook by clicking a button in Excel worksheet, how can you do? This article will introduce a VBA method to achieve it in details.

Send email if button is clicked with VBA code


Send email if button is clicked with VBA code

Please do as follows to send an email through Outlook if a Command Button is clicked in Excel workbook.

1. Insert a Command Button in your worksheet by clicking Developer > Insert > Command Button (ActiveX Control). See screenshot:

2. Right-click the inserted Command Button, then click View Code from the right-clicking menu as below screenshot show.

3. In the opening Microsoft Visual Basic for Applications window, please replace the original code in the Code window with the following VBA script.

VBA code: Send email if button is clicked in Excel

Private Sub CommandButton1_Click()
'Updated by Extendoffice 2017/9/14
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Body content" & vbNewLine & vbNewLine & _
              "This is line 1" & vbNewLine & _
              "This is line 2"
                  On Error Resume Next
    With xOutMail
        .To = "Email Address"
        .CC = ""
        .BCC = ""
        .Subject = "Test email send by button clicking"
        .Body = xMailBody
        .Display   'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

Notes:

1). Please change the email body as you need in the xMailBody line in the code.

2). Replace the Email Address with the recipient email address in line .To = "Email Address".

3). Specify the Cc and Bcc recipients as you need in .CC = “” and .Bcc = “” sections.

4). Change the email subject in line .Subject = "Test email send by button clicking".

4. Press the Alt + Q keys simultaneously to close the Microsoft Visual Basic for Applications window.

5. Turn off the Design Mode by clicking Developer > Design Mode. See screenshot:

From now on, every time you click on the Command Button, an email will be created automatically with specified recipients, subject and body. Please send the email by clicking the Send button.

Note: The VBA code is only working when you use Outlook as your email program.

Easily send email through Outlook based on the fields of created mailing list in Excel:

The Send Emails utility of Kutools for Excel helps to send email through Outlook based on the fields of created mailing list in Excel.
Download and try it now! ( 30-day free trail)


Related articles:


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Comments (73)
Rated 3.5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Hi, I am looking to send data from a pivot table data, can you please help
This comment was minimized by the moderator on the site
Is it possbile to write the script so that when you click the button it will attach itself to the email as above but remove the button as well? so that the copy of the file being emailed no longer has the button on it?
This comment was minimized by the moderator on the site
Just below ".Body = xMailBody" add the following
.Attachments.Add ActiveWorkbook.FullName
This comment was minimized by the moderator on the site
Hi Danie.

I added the portion as you said, but the email with the attached workbook still have the buttons on.
This comment was minimized by the moderator on the site
hi is it possible to set it up so that I dont have to press send - it automatically sends the email????
This comment was minimized by the moderator on the site
Hi,
Please replace the line .Display with .Send in above VBA code.
This comment was minimized by the moderator on the site
it only generates one email and will keep overwriting the contents instead of opening up multiply email drafts.
This comment was minimized by the moderator on the site
Hi all first off a Big thank you, this post is very helpful and works for the attachment. This works for me but no updates are saved in the sheet you have to hit the save button. I would like the attachment to have whatever is currently in the excel sheet.

I can accomplish this by using the excel built in E-mail feature but I wanted a button as I need to hardcode the specific email address.

So in summary I would like to know:

I would like to know if there is a way to: after a user open the excel workbook and made edits if the button will keep the attachment with the updates?
This comment was minimized by the moderator on the site
Good Day,
The code has been optimized. Please have a try and thanks for your comment.

Private Sub CommandButton1_Click()
'Updated by Extendoffice 2017/9/14
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
ActiveWorkbook.Save
xMailBody = "Body content" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"
On Error Resume Next
With xOutMail
.To = "Email Address"
.CC = ""
.BCC = ""
.Subject = "Test email send by button clicking"
.Body = xMailBody
.Attachments.Add ActiveWorkbook.FullName
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
This comment was minimized by the moderator on the site
How can I attach the Active Workbook to the email when i press the button?
This comment was minimized by the moderator on the site
Good day,
Just below ".Body = xMailBody" add the following
.Attachments.Add ActiveWorkbook.FullName
This comment was minimized by the moderator on the site
Teria como em vez de enviar a planilha, enviar as informações em imagem?
This comment was minimized by the moderator on the site
using this VBA code, can I reference cell information within the body of the email? For instance, how would I reference cell values to the code below?

xMailBody = "Body content" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"

Also, using the (.TO = "email address") code. how can I have the "email address" pull in an email address from a cell in the column next to it.


Thanks,
This comment was minimized by the moderator on the site
Is it possible to add a second email button within the same worksheet? When I try to do it, it is connecting to the code from the original email button. thanks.
This comment was minimized by the moderator on the site
Hi,
You need to assign different codes to each button.
This comment was minimized by the moderator on the site
using this VBA code, can I reference cell information within the body of the email? For instance, how would I reference cell values to the code below?

xMailBody = "Body content" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"

Thanks,
This comment was minimized by the moderator on the site
Hi Glenn,
Please apply the following code. Thank you for your comment.
xMailBody = [B5]
This comment was minimized by the moderator on the site
Is it possible to, instead of sending the attachment in the email, to send a hyperlink to the file (located on SharePoint)?
This comment was minimized by the moderator on the site
Hi Laurie,
Can't help you with that. Thank you for your comment.
This comment was minimized by the moderator on the site
Can I reference the value in a cell in the subject with string? Some variation of the below?

.Subject = "New Event:" & Target.Address = "$B$38"
This comment was minimized by the moderator on the site
Hi Abi,
Please change the code to .Subject = "New Event:" & [B38].
Thank you for your comment.
This comment was minimized by the moderator on the site
Hi There,
Can anyone could help me on the following requirement?
I have an excel sheet having two drop down lists.

1 drop down list- list of departments

2 drop down list- category

What i want is when I select a department and category from the lists;(for example if I select "sales" in department and "monthly report" in category)

I need to email the PDF version of that work sheet to the sales team with the email subject is monthly report.

If i select "production" from the department list, the email should go to a group of people in the production.

I appreciate if you can help me on this

Rasike
This comment was minimized by the moderator on the site
Hi Rasike,
Sorry can't help you with that. Welcome to post any question in our forum: https://www.extendoffice.com/forum.html to get more Excel support from out Excel professional or other Excel fans.
This comment was minimized by the moderator on the site
Hi,

How can I get the mail to automatically add my signature from outlook?
This comment was minimized by the moderator on the site
Hi John,
The below VBA code can help you solve the problem. Thanks for your comment.

Private Sub CommandButton1_Click()
'Updated by Extendoffice 2019/6/26
Dim xOutApp As Object
Dim xOutMail As Object
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
With xOutMail
.Display 'or use .Send
.To = "Email Address"
.CC = ""
.BCC = ""
.Subject = "Test email send by button clicking"
.HTMLBody = "This is a test email sending in Excel" & "
" & .HTMLBody
'.Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
This comment was minimized by the moderator on the site
Do you have to be an excel user when pushing the button to send an email? or just the person receiving the email has to be an outlook user?
This comment was minimized by the moderator on the site
Good day,
As the email need to be sent through Outlook after clicking the button in Excel, you must have Outlook installed in your computer to make it work.
This comment was minimized by the moderator on the site
I want to send the excel worksheet in the email but it doesnt send it. How do you write code to send document as email
This comment was minimized by the moderator on the site
Hi marcus,
The below VBA code can help you solving the problem.

Sub SendWorkSheet()
'Update 20190626
Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object
On Error Resume Next
Application.ScreenUpdating = False
Set Wb = Application.ActiveWorkbook
ActiveSheet.Copy
Set Wb2 = Application.ActiveWorkbook
Select Case Wb.FileFormat
Case xlOpenXMLWorkbook:
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
Case xlOpenXMLWorkbookMacroEnabled:
If Wb2.HasVBProject Then
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbookMacroEnabled
Else
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
End If
Case Excel8:
xFile = ".xls"
xFormat = Excel8
Case xlExcel12:
xFile = ".xlsb"
xFormat = xlExcel12
End Select
FilePath = Environ$("temp") & "\"
FileName = Wb.Name & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
With OutlookMail
.To = "skyyang@extendoffice.com"
.CC = ""
.BCC = ""
.Subject = "kte features"
.Body = "Please check and read this document."
.Attachments.Add Wb2.FullName
.Display
'.Send
End With
Wb2.Close
Kill FilePath & FileName & xFile
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
How can I attach the Active Worksheet to the email when I press the button?
This comment was minimized by the moderator on the site
Hi Grant,
Please right click the button and select View Code, then copy below code between the Private Sub and the End Sub lines. Hope I can help. Thanks for your comment.

Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object
On Error Resume Next
Application.ScreenUpdating = False
Set Wb = Application.ActiveWorkbook
ActiveSheet.Copy
Set Wb2 = Application.ActiveWorkbook
Select Case Wb.FileFormat
Case xlOpenXMLWorkbook:
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
Case xlOpenXMLWorkbookMacroEnabled:
If Wb2.HasVBProject Then
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbookMacroEnabled
Else
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
End If
Case Excel8:
xFile = ".xls"
xFormat = Excel8
Case xlExcel12:
xFile = ".xlsb"
xFormat = xlExcel12
End Select
FilePath = Environ$("temp") & "\"
FileName = Wb.Name & Format(Now, "dd-mmm-yy h-mm-ss")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
With OutlookMail
.To = "skyyang@extendoffice.com"
.CC = ""
.BCC = ""
.Subject = "kte features"
.Body = "Please check and read this document."
.Attachments.Add Wb2.FullName
.Display
'.Send
End With
Wb2.Close
Kill FilePath & FileName & xFile
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
This comment was minimized by the moderator on the site
Hi,

Is there a way to change the file name to the current date when it attaches to the email?
This comment was minimized by the moderator on the site
Hi,

I have been working on a sheet, but i can't finish it. I hope u can help me :)

The file its self must be a xltm (template) and it must attach the sheet it self in the mail.

And a auto signature, then i would be very happy.

Thanks in advance /Dr. Nang
This comment was minimized by the moderator on the site
Hi, I run a spreadsheet with 80 individual bar accounts and have used VBA code on this page with much success. However in the body of the email I would like to copy and paste the specific range of cells of an account when sending so the client can have a history. Can you help with the VBA code for this?
This comment was minimized by the moderator on the site
Hi! Thank you so much for this. It's been fantastic help.

Is it possible to send an active work sheet by pressing a button instead of the full workbook?

Thanks!
There are no comments posted here yet
Load More
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations