'Sending a chart in mail body

I'm trying to modify Ron de Bruin's code to send a chart in mail body.

I export the chart and save it as an PNG image, then I modify HTML code to add it to the message.

The code should run on a server and send mails to people in my workplace.

When using MailItem.Display and manually clicking "send" when my message appears, everything works.
When I try to use MailItem.Send I get an icon in the mail body like it tried to attach an image which it couldn't find.

When I send that mail from a server, on a server account, the chart is displayed correctly.
It doesn't work when I try to send it on "local" computers.

Sub wyslij()

    NameOfThisFile = ActiveWorkbook.Name
    
    Dim rng As Range
    Dim dataminus1, dataminus2 As Date
    Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")
    
    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)
    
    Set rng = Nothing
    Set rng = Sheets(2).Range("E1:P13")
    
    olMail.To = "xxx@xxx" 
    olMail.CC = "xxxx@xxx"
    olMail.Subject = "xxxx"
    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 & "\Chart.png", Filtername:="PNG"
        End If
    Next cht
    
Err_Chart:
    If Err <> 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$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    
    '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("WplatyFinal.xlsm").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("Scripting.FileSystemObject")
        
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    
    RangetoHTML = ts.readall
    ts.Close
    
    If ChartEx Then
        RangetoHTML = RangetoHTML & "<img src ='" & ActiveWorkbook.Path & "\Chart.png" & "'>"
    End If
        
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")
    
    '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

I tried to use the Wait function directly after the Send method.



Solution 1:[1]

Excellent! I couldn't manage to attach the active workbook into the mail. I tried to add the code .Attachments.Add (ActiveWorkbook.FullName) but didn't work, I received a message saying that the file is in use, and sometimes Runtime error 424 - Object required

With olMail
    .To = "[email protected]"
    '.CC = "xxxx@xxx"
    .Subject = "xxxx"
    .Attachments.Add (ActiveWorkbook.FullName) ' this i added

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eliton Elias