简体   繁体   中英

Using Excel VBA to send email with chart object in body - Office 2013

I have used Excel VBA to create an email pulling information from excel, and then sending email directly from excel. But, I want to also be able to include a chart object in the body of the email. I have come across some examples that convert the chart to an image and then embed the image in the body of the email using HTML. I would like to try and keep this as an excel chart object, not an image.

I have also found some code samples that make use the "ActiveChart.ChartArea.Copy" and using either sendkeys or some other line of code to paste in the chart, but it doesn't seem to be working either. The only being pasted is code from the VBA module.

If someone can tell me what I may be missing, I would greatly appreciate it. The code below shows what I have tried. I've left in items I've tried based on different things I have read or tried, but commented out anything that doesn't seem to work.

Private Sub CommandButton2_Click()

Dim mainWB As Workbook
Dim SendID
Dim CCID
Dim Subject
Dim Body

On Error GoTo Handler

Set myOutlook = GetObject(, "Outlook.Application")
Set myMessage = myOutlook.CreateItem(olMailItem)
Set mainWB = ActiveWorkbook

SendID = mainWB.Sheets("Mail").Range("B1").Value
CCID = mainWB.Sheets("Mail").Range("B2").Value
Subject = mainWB.Sheets("Mail").Range("B3").Value
Body = mainWB.Sheets("Mail").Range("B4").Value

ActiveChart.ChartArea.Copy

With myMessage
    .To = SendID
    If CCID <> "" Then
        .CC = CCID
    End If
    .Subject = Subject
   'ActiveChart.ChartArea.Copy
    .Body = Body
        .Display
   ' Set wEditor = o.activeInspector.WordEditor
   ' wEditor.Application.Selection.Paste

End With

SendKeys "^{END}"
SendKeys "^({v})", True

Set myOutlook = Nothing
Set myMessage = Nothing

Exit Sub

Handler:
Set myOutlook = CreateObject("Outlook.Application")
Err.Clear
Resume Next

End Sub

Email (at the time of writing) only support Images (or animated gif). Your issue is why we've built Image-Charts so you can easily embed charts into your email.

Just generate an URL (see our documentation ), embed it into your email ( <img src="https://image-charts.com/chart?[...]" ) and you are done!

Bonus, it also support gifs and has a free-tier forever :)

极地面积图图像

(note: it's a drop-in replacement for the deprecated Google Image Charts, I'm the creator of Image-Charts)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM