Search code examples
excelvbaoutlook

Adding a chart as an attachment or part of the body in an email via Excel VBA


I want to attach a chart in the email when the user clicks on a button.

The code isn't adding the chart.

The naming is correct and I am not receiving any errors (except ones I've implemented to help test).

If ChartNameLine = "" Then
    GoTo ErrorMsgs
Else
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xChartName As String
    Dim xPath As String
    Dim xChart As ChartObject
    Dim xChartPath As String
    
    On Error Resume Next
    xChartName = Application.InputBox("Please Enter the Chart name: ", "KuTools for Excel", , , , , , 2)
    'xChartName = ChartNameLine
    Set xChart = Worksheets(.HTMLBody).ChartObjects(xChartName)
    xChart.Chart.ChartArea.Copy
    
    errorCode = 101
    'If xChart Is Nothing Then GoTo ErrorMsgs
    
    xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xChartPath = ThisWorkbook.path & "\" & Environ("USERNAME") & VBA.Format(VBA.Now(), "DD_MM_YY_HH_MM_SS") & ".bmp"
    xPath = "<p align='Left'><img src= " / "cid:" & Mid(xChartPath, InStrRev(xChartPath, "\") + 1) & """ width = 800 height = 500> <br> <br>"
    xChart.Chart.Export xChartPath
    With xOutMail
        .To = ToLine
        .Subject = SubjectLine
        .Attachments.Add xChartPath
        .HTMLBody = xPath
        .Display
    End With
    Kill xChartPath
    'Set xOutMail = Nothing
    'Set xOutApp = Nothing
End If

Using code from "Extend Office"


Solution

  • Create a chart in a new workbook with a sheet named "test". The chart should be named "Chart 1".

    With no other code in the new workbook.

    Option Explicit ' Consider this mandatory
    ' Tools | Options | Editor tab
    ' Require Variable Declaration
    ' If desperate declare as Variant
    
    Sub AddWorksheetTestChartToMail()
        
        Dim xOutApp As Object
        Dim xOutMail As Object
        
        Dim xChartName As String
        Dim xPath As String
        Dim xChart As ChartObject
        Dim xChartPath As String
                
        xChartName = "Chart 1"
        
        ' "test", not .HTMLBody
        Set xChart = Worksheets("test").ChartObjects(xChartName)
        
        xChart.Chart.ChartArea.Copy
        
        ' Set was missing
        Set xOutApp = CreateObject("Outlook.Application")
        Set xOutMail = xOutApp.CreateItem(0)
        
        xChartPath = ThisWorkbook.Path & "\" & Environ("USERNAME") & VBA.Format(VBA.Now(), "DD_MM_YY_HH_MM_SS") & ".bmp"
        Debug.Print xChartPath
        
        ' suggested fix in comment on the question post - src=""cid:"
        xPath = "<p align='Left'><img src=""cid:" & Mid(xChartPath, InStrRev(xChartPath, "\") + 1) & """  width=700 height=500 > <br> <br>"
        
        xChart.Chart.Export xChartPath
        
        With xOutMail
            .To = "ToLine"
            .Subject = "SubjectLine"
            .Attachments.Add xChartPath
            .HTMLBody = xPath
            .Display
        End With
        
        Kill xChartPath
    
    End Sub