title: Sending a chart in mail body
body: <p>I'm trying to modify Ron de Bruin's code to send a chart in mail body.</p>
  <p>I export the chart and save it as an PNG image, then I modify HTML code to add it to the message.</p>
  <p>The code should run on a server and send mails to people in my workplace.</p>
  <p>When using <code>MailItem.Display</code> and manually clicking &quot;send&quot; when my message appears, everything works.<br />
  When I try to use <code>MailItem.Send</code> I get an icon in the mail body like it tried to attach an image which it couldn't find.</p>
  <p>When I send that mail from a server, on a server account, the chart is displayed correctly.<br />
  It doesn't work when I try to send it on &quot;local&quot; computers.</p>
  <pre class="lang-vb prettyprint-override"><code>Sub wyslij()
  
      NameOfThisFile = ActiveWorkbook.Name
      
      Dim rng As Range
      Dim dataminus1, dataminus2 As Date
      Dim olApp As Outlook.Application
      Set olApp = CreateObject(&quot;Outlook.Application&quot;)
      
      Dim olMail As Outlook.MailItem
      Set olMail = olApp.CreateItem(olMailItem)
      
      Set rng = Nothing
      Set rng = Sheets(2).Range(&quot;E1:P13&quot;)
      
      olMail.To = &quot;xxx@xxx&quot; 
      olMail.CC = &quot;xxxx@xxx&quot;
      olMail.Subject = &quot;xxxx&quot;
      olMail.HTMLBody = RangetoHTML(rng)
      olMail.Display
      'olMail.Send
      
      'Delete file after sending a mail
      'Call DeleteFile(Path)
      
  End Sub
      
  Sub Save_ChartAsImage()
      
      ChartEx = False
      
      Dim cht As ChartObject
      
      For Each cht In ActiveSheet.ChartObjects
          If cht.TopLeftCell.Column = ChartCol And cht.TopLeftCell.Row = ChartRow Then
              ChartEx = True
              On erRROR GoTo Err_Chart
              cht.Chart.Export Filename:=ActiveWorkbook.Path &amp; &quot;\Chart.png&quot;, Filtername:=&quot;PNG&quot;
          End If
      Next cht
      
  Err_Chart:
      If Err &lt;&gt; 0 Then
          Debug.Print Err.Description
          Err.Clear
      End If
  End Sub
  
  Function RangetoHTML(rng As Range)
  
      ' Changed by Ron de Bruin 28-Oct-2006
      ' Working in Office 2000-2013
      
      Dim fso As Object
      Dim ts As Object
      Dim TempFile As String
      Dim TempWB As Workbook
      
      TempFile = Environ$(&quot;temp&quot;) &amp; &quot;\&quot; &amp; Format(Now, &quot;dd-mm-yy h-mm-ss&quot;) &amp; &quot;.htm&quot;
      
      'Copy the range and create a new workbook to past the data in
      rng.Copy
      Set TempWB = Workbooks.Add(1)
      
      With TempWB.Sheets(1)
          .Cells(1).PasteSpecial Paste:=8
          .Cells(1).PasteSpecial xlPasteValues, , False, False
          '.Cells(1).PasteSpecial xlPasteAll
          .Cells(1).PasteSpecial xlPasteFormats, , False, False
      
          .Cells(1).Select
          Application.CutCopyMode = False
          On Error Resume Next
          .DrawingObjects.Visible = True
          .DrawingObjects.Delete
          On Error GoTo 0
      End With
      
      'kopiujemy wykres z poprzedniego działu
      'Workbooks(&quot;WplatyFinal.xlsm&quot;).Activate
      Workbooks(NameOfThisFile).Activate
      Call Save_ChartAsImage
      
      TempWB.Activate
      TempWB.Sheets(1).Select
      
      'Publish the sheet to a htm file
      With TempWB.PublishObjects.Add( _
           SourceType:=xlSourceRange, _
           Filename:=TempFile, _
           Sheet:=TempWB.Sheets(1).Name, _
           Source:=TempWB.Sheets(1).UsedRange.Address, _
           HtmlType:=xlHtmlStatic)
          .Publish (True)
      End With
      
      'Read all data from the htm file into RangetoHTML
      Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
          
      Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
      
      RangetoHTML = ts.readall
      ts.Close
      
      If ChartEx Then
          RangetoHTML = RangetoHTML &amp; &quot;&lt;img src ='&quot; &amp; ActiveWorkbook.Path &amp; &quot;\Chart.png&quot; &amp; &quot;'&gt;&quot;
      End If
          
      RangetoHTML = Replace(RangetoHTML, &quot;align=center x:publishsource=&quot;, _
                            &quot;align=left x:publishsource=&quot;)
      
      'Close TempWB
      TempWB.Close savechanges:=False
      
      'Delete the htm file we used in this function
      Kill TempFile
      
      Set ts = Nothing
      Set fso = Nothing
      Set TempWB = Nothing
  End Function
  </code></pre>
  <p>I tried to use the Wait function directly after the Send method.</p>