So im working on this project where i want to attach the specific chart in the email when the user clicks on a button. Im using vba tools to code this, but for some reason the code isn't adding the chart, the naming is correct and i am not receiving any errors (Except ones ive implemented to help test).
If ChartNameLine = "" Then
GoTo ErrorMsgs
Else
Dim xOutApp As Object
Dim xOutMail As Object
Dim xChartName As String
Dim xPath As String
Dim xChart As ChartObject
Dim xChartPath As String
On Error Resume Next
xChartName = Application.InputBox("Please Enter the Chart name: ", "KuTools for Excel", , , , , , 2)
'xChartName = ChartNameLine
Set xChart = Worksheets(.HTMLBody).ChartObjects(xChartName)
xChart.Chart.ChartArea.Copy
errorCode = 101
'If xChart Is Nothing Then GoTo ErrorMsgs
xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
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 = 800 height = 500> <br> <br>"
xChart.Chart.Export xChartPath
With xOutMail
.To = ToLine
.Subject = SubjectLine
.Attachments.Add xChartPath
.HTMLBody = xPath
.Display
End With
Kill xChartPath
'Set xOutMail = Nothing
'Set xOutApp = Nothing
End If
Using code from "Extend Office"
If someone could help with what im doing wrong that'd be great.
First of all, the HTMLBody property returns or sets a string representing the HTML body of the specified item. If you need to add an image generated in Excel you most probably need to insert it at some point in the message, not substitute the whole message by setting it to the paragraph HTML tag. So, find a suitable place in the HTML document and insert the generated HTML piece there instead of replacing the whole message body.
Second, make sure a correct image is generated and saved to the disk. And there were no problems with image generation process from the Excel side.
Third, you may need to set the PR_ATTACH_CONTENT_ID property on the attached image so Outlook could easily recognize the embedded image.
Create a chart in a new workbook with a sheet named "test". The chart should be named "Chart 1".
With no other code in the new workbook.
Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant
Sub AddWorksheetTestChartToMail()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xChartName As String
Dim xPath As String
Dim xChart As ChartObject
Dim xChartPath As String
xChartName = "Chart 1"
' "test", not .HTMLBody
Set xChart = Worksheets("test").ChartObjects(xChartName)
xChart.Chart.ChartArea.Copy
' Set was missing
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xChartPath = ThisWorkbook.Path & "\" & Environ("USERNAME") & VBA.Format(VBA.Now(), "DD_MM_YY_HH_MM_SS") & ".bmp"
Debug.Print xChartPath
' suggested fix in comment on the question post - src=""cid:"
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 = "ToLine"
.Subject = "SubjectLine"
.Attachments.Add xChartPath
.HTMLBody = xPath
.Display
End With
Kill xChartPath
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.