Add a chart to the body of an email

NewOrderFac33

Well-known Member
Joined
Sep 26, 2011
Messages
1,275
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
Good morning,

I have been using Ron De Bruin's email generating code for a while now:

Code:
Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    With OutMail
        .To = EmailAddress
        .CC = ""
        .BCC = ""
        .Subject = "JIRA Releases in Released to UAT status"
        .HTMLBody = MsgBody
        '.attachments.Add "C:\Pete's Stuff\Sample Attachment.xlsx"
        .Display
        '.Send
    End With
    
    Set OutMail = Nothing
    Set OutApp = Nothing

but would like to know if there is any way in which I can embed a named worksheet chart (not a chart tab) into the body of the email before it is previewed or sent?

Thanks in advance for your help.

Pete
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Classic case of "Google it first" (although this adds the chart as an an attachment, rather than embedding it in the email body, so still open to suggestions)

Code:
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
    
'File path/name of the gif file
Dim FName As String
FName = Environ$("temp") & "\ReleasedToUAT.gif"

'EITHER:
'Save chart sheet with the name "Chart" as GIF file
'ActiveWorkbook.Sheets("Chart").Export FileName:=FName, FilterName:="GIF"
  
'OR:
'Save worksheet chart with the name "RTUATChart" as GIF file
ActiveWorkbook.Worksheets("Chart").ChartObjects("RTUATChart").Chart.Export FileName:=FName, FilterName:="GIF"
    
With OutMail
    .To = EmailAddress
    .CC = ""
    .BCC = ""
    .Subject = "JIRA Releases in Released to UAT status"
    .HTMLBody = MsgBody
    '.attachments.Add "C:\Pete's Stuff\Sample Attachment.xlsx"
    .attachments.Add FName
    .Display
    '.Send
End With
    
'Delete the gif file
Kill FName
    
Set OutMail = Nothing
Set OutApp = Nothing

Pete :)
 
Last edited:
Upvote 0
This works, pasting the chart in immediately before the rest of MsgBody. HOWEVER, it now throws up the message:

"A program is trying to access e-mail address information stored in Outlook. If this is unexpected, click Deny and verify your antivirus software is up to date."

There are three buttons displayed, "Allow", "Deny" and "Help". If I click "Allow", my code continues to work fine.

Is there any way in which I can suppress this message?

Code:
Dim OutApp As Object
    Dim OutMail As Object
    Dim wEditor As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    
    'Set path/name of the GIF file
    Dim GIFName As String
    GIFName = Environ$("temp") & "\ReleasedToUAT.GIF"
    
    'Save chart sheet with the name "Chart" as GIF file
    'ActiveWorkbook.Sheets("Chart").Export FileName:=FName, FilterName:="GIF"
    
    'Save Worksheet chart with the name "RTUATChart" as GIF file
    ActiveWorkbook.Worksheets("Chart").ChartObjects("RTUATChart").Chart.Export FileName:=GIFName, FilterName:="GIF"
    
    
    Application.DisplayAlerts = False
    Sheets("Chart").ChartObjects("RTUATChart").Activate
    ActiveChart.PlotArea.Select
    ActiveChart.ChartArea.Copy
    
    With OutMail
        .To = EmailAddress
        .CC = ""
        .BCC = ""
        .Subject = "JIRA Releases in Released to UAT status"
        .HTMLBody = MsgBody
        '.attachments.Add "C:\Pete's Stuff\Sample Attachment.xlsx"
        .attachments.Add GIFName
        .Display
        '.Send
    End With
    
    Set wEditor = OutApp.ActiveInspector.wordEditor
    wEditor.Application.Selection.Paste
    Application.CutCopyMode = False
    
    'Delete the GIF file
    Kill GIFName
    
    Set OutMail = Nothing
    Set OutApp = Nothing
    Set wEditor = Nothing
    Application.DisplayAlerts = True
    Sheets("Database").Activate

Regards

Pete
 
Upvote 0
Hi Pete,

Here is the one more approach. I have added extra space for HTML tags. You need to Replace "< " to "<" and " >" to ">" before using the below code.

Code:
Sub Chart_Email_Body()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim StartMsg As String, Msgbody As String, endmsg As String
    
'//File path/name of the gif file
Dim FName As String
FName = Environ$("temp") & "\ReleasedToUAT.gif"

'//Save Chart named "RTUATChart" as gif file
ActiveWorkbook.Worksheets("Chart").ChartObjects("RTUATChart").Chart.Export Filename:=FName, FilterName:="GIF"

StartMsg = "< font size='5' color='black' > Hi There," & "< br > < br >" & "Please find the chart below: " & "< br > < br > < /font >"
Msgbody = "< p align='Left' >< img src=" & FName & """  width=500 height=300  > < br > < br >"
endmsg = "< font size='4' color='black' > Many Thanks," & "< br >" & "vds1" & "< br > < br > < /font >"

'//Set wEditor = OutApp.ActiveInspector.wordeditor
With OutMail
    .To = EmailAddress
    .CC = ""
    .BCC = ""
    .Subject = "JIRA Releases in Released to UAT status"
    .HTMLBody = StartMsg & Msgbody & endmsg
    '.attachments.Add "C:\Pete's Stuff\Sample Attachment.xlsx"
    '.attachments.Add FName
    .Display
    '.Send
End With

'//Delete the gif file
Kill FName

Set OutMail = Nothing
Set OutApp = Nothing

End Sub
 
Last edited:
Upvote 0
VDS1 - this is brilliant - exactly what I wanted! Thank you VERY much indeed!

Pete :)
 
Upvote 0

Forum statistics

Threads
1,214,427
Messages
6,119,419
Members
448,895
Latest member
omarahmed1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top