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.
- Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
- More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
- Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
- Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
- Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
- Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
- More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.
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 = "email@example.com" .Subject = "Add Chart in outlook mail body" .Attachments.Add xChartPath .HTMLBody = xStartMsg & xPath & xEndMsg .Display 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 = "firstname.lastname@example.org" 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.
- How to automatically send email based on cell value in Excel?
- How to insert signature into Outlook email when sending by vba in Excel?
- How to send an email at specific time of a day in Excel?
- How to send email with HTML email body in Excel?
- How to send email with specified bold/size/color/underline text format in Excel?
- How to send email without Outlook in Excel?
You are guest
or post as a guest, but your post won't be published automatically.
- To post as a guest, your comment is unpublished.· 9 months agohola como puede enviar por correo, una tabla dinámica, y no un gráfico
- To post as a guest, your comment is unpublished.· 1 years agowhen 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.· 10 months agoThe 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 :]