VBA for email image - interesting issue!

sn281

New Member
Joined
Dec 13, 2011
Messages
26
Hi all - thanks for reading.

I have some code (pasted below) that creates an email pastes a range from an Excel document as an image into the body as well as the range as HTML into the body and as an attachment.
The weird thing I am experiencing is that when I view the email (before sending) the image is a box with a cross in it saying:
"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"
But then when I send the email, the image is there?!?! Can anyone explain what on earth is going on there, and if there is a way I can see the image before sending (so I can check it looks okay).

The second problem is that before when I manually copy and pasted as a bitmap, it was excellent quality, whereas now it is rubbish. On a mobile device it is impossible to read (the image in the body and the attachment). Does anyone know a solution to this?

Thanks so much in advance for your help.
sn281


Code:
Sub Mail_Selection_Range_Outlook_Body()

    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object


    Set rng = Nothing
    On Error Resume Next
    Set rng = Sheets("Email").Range("B2:S35")
    On Error GoTo 0


    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If


    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = Date & " Curves"
        .HTMLBody = "Hi all, Please see below for " & Date & " curves."
        
              
        
        'first we create the image as a bmp file
        Call createbmp("Email", "B2:S35", "PCB Process Master")
        'we attached the embedded image with a Position at 0 (makes the attachment hidden) or 1 (not hidden)
        TempFilePath = Environ$("temp") & "\"
        .Attachments.Add TempFilePath & "PCB Process Master.bmp", olByValue, 1
                
        'this is where to edit size
PLEASE NOTE THIS LINE BELOW ONLY HAS THE "---" IN TO DISPLAY IT NEATLY ON THIS FORUM - PLEASE DELETE THE --- TO VIEW THE CODE PROPERLY

        .HTMLBody = .HTMLBody --- & <---img src---=---'cid:---PCB Process Master.---bmp'" & "width='1150' height='700'>---
---"      



  
        .HTMLBody = .HTMLBody & RangetoHTML(rng)
        .HTMLBody = .HTMLBody & "Kind regards, Stuart"
        .Display
    End With
    On Error GoTo 0


    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With


    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub




Function RangetoHTML(rng As Range)
    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 xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With


    '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
    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








Sub createbmp(Namesheet As String, nameRange As String, nameFile As String)
    ThisWorkbook.Activate
    Worksheets(Namesheet).Activate
    Set Plage = ThisWorkbook.Worksheets(Namesheet).Range(nameRange)
    Plage.CopyPicture
    With ThisWorkbook.Worksheets(Namesheet).ChartObjects.Add(Plage.Left, Plage.Top, Plage.Width, Plage.Height)
        .Activate
        .Chart.Paste
        .Chart.Export Environ$("temp") & "\" & nameFile & ".bmp", "bmp"
    End With
    Worksheets(Namesheet).ChartObjects(Worksheets(Namesheet).ChartObjects.Count).Delete
Set Plage = Nothing
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"
It looks like you're missing an openning quote. Does this help?

(Note that you'll need to replace [ with < and ] with >.)

Code:
.HTMLBody = .HTMLBody & "[img src='cid:PCB Process Master.bmp' width='1150' height='700']"

Hope this helps!
 
Last edited:
Upvote 0
Hi Domenic,

Thanks for responding ... the square brackets is a much better idea than mine!
I have checked through my code and the quote is actually there, must've been a copy and paste error.

The weird thing is the code does fully work, just in poor quality and by not allowing me to preview the actual image.

I'm really confused here.....


Thanks,
sn281
 
Upvote 0

Forum statistics

Threads
1,215,009
Messages
6,122,674
Members
449,091
Latest member
peppernaut

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