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