Trying to embed a picture in the body of an email while in sleep mode.

ToyoMike

New Member
Joined
Jul 5, 2020
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,

I've been trying to use different methods of sending off an email report in the late evening while my laptop is in sleep mode. I am using the Task Scheduler to wake the laptop and open an Excel file which upon opening runs a macro to perform various reporting tasks and then sends an email report of the results. Everything has been working with the exception of embedding a picture of a chart into the body of the email. I've tried to add a few different pieces of code to force the procedure but nothing has worked. I'm getting the email sent with an empty box with the red x in the corner stating that the linked image cannot be displayed and that the file may have been removed, renamed or deleted.

The VBA works when I run the code manually and produces the expected email results with embedded picture but not when I'm away and it fires off during sleep mode. What I have noticed is that the second part of the code that is supposed to create an image does not seem to work during the auto run process. There is no image created in the Temp folder.

Can someone take a look at my code and give me some pointers? Thank you.


VBA Code:
Sub sendMail()
    Dim FilePath As String
    Dim Outlook As Object
    Dim OutlookMail As Object
    Dim HTMLBody As String
    Dim rng As Range
    On Error Resume Next
    Set rng = ThisWorkbook.Worksheets("Graph").Range("B4:S44")
    If rng Is Nothing Then Exit Sub
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    
    Set Outlook = CreateObject("outlook.application")
    Set OutlookMail = Outlook.CreateItem(olMailItem)
    Call createImage(ActiveSheet.Name, rng.Address, "EmailReport")
    FilePath = Environ$("temp") & "\"
    HTMLBody = "<span LANG=EN>" _
            & "<p class=style1><span LANG=EN><font FACE=Calibri SIZE=3>" _
            & "Hello Joe," _
            & "<br><br>" _
            & "Please refer to the gragh below for today's Distributor Support database update.<br> " _
            & "<br>" _
            & "<img src=""cid:EmailReport.jpg""height=700 width=950>" _
            & "<br>" _
            & "<br>Have a great day," _
            & "<br><br>" _
            & "Distributor Support RPA</font></span>"
    With OutlookMail
        .To = Worksheets("Graph").Range("V4")
        .CC = Worksheets("Graph").Range("V5")
        .Subject = Worksheets("Graph").Range("V7")
        .Attachments.Add ("C:\Users\020885\Desktop\Email Tracker.xlsm")
        .HTMLBody = HTMLBody & "<br><br>" & .HTMLBody
        .Attachments.Add FilePath & "EmailReport.jpg", olByValue
        .Save
        .Send
    End With
    
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

VBA Code:
Sub createImage(SheetName As String, rngAddrss As String, nameFile As String)
    Dim rngJpg As Range
    Dim Shape As Shape
    
    Set rngJpg = ThisWorkbook.Worksheets("Graph").Range("B4:S44")
    rngJpg.CopyPicture
    
            n = 1 ' Set counter to 1
            Do Until n > 3 'Attempt paste function three times before falling out
            If n < 3 Then  ' suspend normal error handling
            On Error Resume Next
            Else
            On Error GoTo 0 ' on last attempt, reinstate normal error handling
            End If
            
    With ThisWorkbook.Worksheets("Graph").ChartObjects.Add(rngJpg.Left, rngJpg.Top, rngJpg.Width, rngJpg.Height)
        .Activate
        For Each Shape In ActiveSheet.Shapes
            Shape.Line.Visible = msoFalse
        Next
        .Chart.Paste
        .Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG"
    End With
    
            If Err.Number = 0 Then
            On Error GoTo 0 'reinstate normal error handling
            Exit Do   ' Exit if no error encountered
            End If
            n = n + 1  ' Increment counter and repeat the Do Until Loop
            DoEvents
            Loop
            On Error GoTo 0  ' Just to make sure that normal error handling is reinstated
       
    
   Worksheets("Graph").ChartObjects(Worksheets("Graph").ChartObjects.Count).Delete
Set rngJpg = Nothing

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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