Note: The other languages of the website are Google-translated. Back to English
Log in  \/ 
Register  \/ 


How to send a specific chart in an email with vba in Excel?

You may know how to send an email through Outlook in Excel with VBA code. However, do you know how to attach a specific chart in a certain worksheet into the body of the email? This article wills show you the method to solve this problem.

Send a specific chart in an email in Excel with VBA code

Send a specific chart in an email in Excel with VBA code

Please do as follows to send a specific chart in an email with VBA code in Excel.

1. In the worksheet contains the chart you want to attach in the email body, press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, please click Insert > Module. Then copy below VBA code into the Code window.

VBA code: Send a specific chart in an email in Excel

Sub mailHTMLsend()
'Updated by Extendoffice 2018/3/5
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xStartMsg As String
    Dim xEndMsg As String
    Dim xChartName As String
    Dim xChartPath As String
    Dim xPath As String
    Dim xChart As ChartObject
    On Error Resume Next
    xChartName = Application.InputBox("Please enter the chart name:", "KuTools for Excel", , , , , , 2)
    If xChartName = "" Then Exit Sub
    Set xChart = Sheets("Sheet1").ChartObjects(xChartName)
    If xChart Is Nothing Then Exit Sub
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xStartMsg = "<font size='5' color='black'> Good Day," & "<br> <br>" & "Please find the chart below: " & "<br> <br> </font>"
    xEndMsg = "<font size='4' color='black'> Many Thanks," & "<br> <br> </font>"
    xChartPath = ThisWorkbook.Path & "\" & Environ("USERNAME") & VBA.Format(VBA.Now(), "DD_MM_YY_HH_MM_SS") & ".bmp"
    xPath = "<p align='Left'><img src="/"cid:" & Mid(xChartPath, InStrRev(xChartPath, "\") + 1) & """  width=700 height=500 > <br> <br>"
    xChart.Chart.Export xChartPath
    With xOutMail
        .To = ""
        .Subject = "Add Chart in outlook mail body"
        .Attachments.Add xChartPath
        .HTMLBody = xStartMsg & xPath & xEndMsg
    End With
    Kill xChartPath
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

Note: In the code, please change the recipient’s email address and the email subject in line .To = "" and line .Subject = "Add Chart in outlook mail body" , Sheet1 is the sheet which contains the chart you want to send, please change it to your own.

3. Press the F5 key to run the code. In the opening Kutools for Excel dialog box, enter the name of the chart you will attach in the email body, and then click the OK button. See screenshot:

Then an email is created automatically with the specified chart showing in the email body as below screenshot shown. Please click the Send button to send this email.

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
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    YOussef · 7 months ago
    It includes the chart as an attachment. Do you have any idea how to include it as a picture in the mail body itself. 
    Thank you,
  • To post as a guest, your comment is unpublished.
    Nitin Sharma · 10 months ago
    There is error in the code : "\") + 1) & """ width=700 height=50
    In the bold text the middle one should be a single inverted comma

  • To post as a guest, your comment is unpublished.
    Aaron Manqueo · 2 years ago
    hola como puede enviar por correo, una tabla dinámica, y no un gráfico
  • To post as a guest, your comment is unpublished.
    leigh · 2 years ago
    when i enter the chart name the mail doesn't generate the dialog box just closes, any idea what i have done wrong? I have followed each step
    • To post as a guest, your comment is unpublished.
      Borges · 2 years ago
      The issue is that we can not set names for Chart Objects like tables. You need to pass the integer ID to work. For instance, if you only have 1 chart in the "Sheet1", trying passing the value 1 when the msgbox shows up.

      PS: sorry for the bad english :]