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.
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 = "firstname.lastname@example.org" .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 = "email@example.com" 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.· 7 months agoIt 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,Youssef
- To post as a guest, your comment is unpublished.· 10 months agoThere is error in the code : "\") + 1) & """ width=700 height=50In the bold text the middle one should be a single inverted comma
- To post as a guest, your comment is unpublished.· 2 years agohola como puede enviar por correo, una tabla dinámica, y no un gráfico
- To post as a guest, your comment is unpublished.· 2 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.· 2 years 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 :]