How to insert chart or graph into body of Outlook

2019-09-02 15:45发布

There was a question posted here about how to use VBA to insert a chart into the body of an Outlook email that I drafted an answer to but has since disappeared.

I thought there would be a simpler way to do this without Sendkeys but looks like I was mistaken!

Anyway, I thought I'd post the answer in case the original poster is still looking, or if anyone else needs it, or even if someone can come up with a better solution.

1条回答
Animai°情兽
2楼-- · 2019-09-02 16:42
Sub charttoemail()

Dim myOutlook As Outlook.Application
Dim myMessage As Outlook.MailItem

On Error GoTo Handler
Set myOutlook = GetObject(, "Outlook.Application")
Set myMessage = myOutlook.CreateItem(olMailItem)

'Copy the chart
ActiveChart.ChartArea.Copy

    With myMessage

        .To = "steven@test.com"
        .Subject = "Here's your chart"
        .BodyFormat = olFormatHTML
        .Body = "Hi, Here's your chart" & vbCr & vbCr
        'Display to view the email and send to automatically fire it off
            .Display
            '.Send

    End With

'First key moves cursor to end of email and the second
'pastes the copied chart
SendKeys "^{END}"
SendKeys "^({v})", True

Set myOutlook = Nothing
Set myMessage = Nothing

Exit Sub

'If Outlook is not open, open it
Handler:

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

End Sub
查看更多
登录 后发表回答