Outlook image error when pasting a range as a pciture

mecerrato

Board Regular
Joined
Oct 5, 2015
Messages
174
Office Version
  1. 365
Platform
  1. Windows
I have this code that emails a range as a picture via outlook. The problem is that the recipients are getting the outlook error "The linked image cannot be displayed. The file may have been moved, renamed, or deleted. Verify that the link points to the correct file and location."

Anyone know how I can fix this?

1631898786175.png

VBA Code:
Sub RectangleRoundedCorners1_Click()
ActiveSheet.Unprotect Password:="Mortgage1"
        Dim TempFilePath As String
        Dim xOutApp As Object
        Dim xOutMail As Object
        Dim xHTMLBody As String
        Dim xRg As Range
        On Error Resume Next
        Set xRg = ActiveSheet.Range("C5:F17")
        If xRg Is Nothing Then Exit Sub
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
 ActiveSheet.Shapes("Rectangle: Rounded Corners 13").Visible = False
 
        Set xOutApp = CreateObject("outlook.application")
        Set xOutMail = xOutApp.CreateItem(olMailItem)
        Call createJpg(ActiveSheet.Name, xRg.Address, "DashboardFile")
        TempFilePath = Environ$("temp") & "\"
        xHTMLBody = "<span LANG=EN>" _
                & "<p class=style2><span LANG=EN><font FACE=Calibri SIZE=3>" _
                & "<img src='cid:DashboardFile.jpg'>" _
'& "<br> "

        StrBody = "<br />" & "<b><FONT SIZE = 5><font color=red>Rates are subject to change without notice</b></FONT SIZE = 5></font color=red>"
        
        With xOutMail
            .Subject = "Bench Mark Rates" & " " & Date
            .HTMLBody = xHTMLBody & StrBody
          .Attachments.Add TempFilePath & "DashboardFile.jpg", olByValue
            .To = "mecerrato@hotmail.com"
            .cc = "mcerrato@loandepot.com"
            .Display
        End With
ActiveSheet.Shapes("Rectangle: Rounded Corners 13").Visible = True
ActiveSheet.Protect Password:="Mortgage1"
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Given how your code looks, I suspect you haven't referenced Outlook's Object Library within VBE. If so, then Outlook's enumerations have not been exposed. At compile time VBA sees the used enumeration names as variables with procedure scope and declares them itself (implicit declaration). They then become variables of the generic Variant vartype and remain empty.

The moment such a variable is actually used at run-time, VBA's implicit vartype conversion kicks in, which then chooses the most likely vartype. In both cases within your code VBA returns a Long vartype, initialized to 0 (zero). Coincidentally, the xlMailItem enumeration equals 0, so it works fine here. The olByValue enumeration, on the other hand, equals 1, so here the code fails, since your (empty) olByValue variable will be converted to 0.

If this line
VBA Code:
.Attachments.Add TempFilePath & "DashboardFile.jpg", olByValue

is changed like this, it's most likely to work.
VBA Code:
.Attachments.Add TempFilePath & "DashboardFile.jpg", 1

In this regard, I would recommend using the Option Explicit statement at the top of every code module. VBE's compiler then warns you beforehand that something is not right.
 
Upvote 0

Forum statistics

Threads
1,214,996
Messages
6,122,636
Members
449,092
Latest member
bsb1122

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