Hi all, a co-worker has moved on and the ownership of a VBA routine that copies a range of cells with Excel and turns that range into a jpeg to added into an email has been passed to me.
The routine loops through a list of managers which changes the data in the range for each new email. The problem that I've been having is that some of the emails contain an empty frame where the picture should be. It doesn't happen for every email, but I've had to set the routine to .display instead of .send and manually check each email and send them manually.
Also the routine falls over occasionally in the Sub createJpg exec at the line for Plage.CopyPicture
Hopefully one of you good peeps can spot something or have a different way of adding the jpeg to the email. Thanks
here's the full code
The routine loops through a list of managers which changes the data in the range for each new email. The problem that I've been having is that some of the emails contain an empty frame where the picture should be. It doesn't happen for every email, but I've had to set the routine to .display instead of .send and manually check each email and send them manually.
Also the routine falls over occasionally in the Sub createJpg exec at the line for Plage.CopyPicture
Hopefully one of you good peeps can spot something or have a different way of adding the jpeg to the email. Thanks
here's the full code
Code:
Sub Send_eMail()
Dim Seldate As Date
Dim TempFilePath As String, selectedouc As String
Dim thisWB As Workbook
Dim shtname As String, selemail As String
Dim EmailFrm As String, FirstName As String
Dim EmailExtraMessage1 As String, EmailExtraMessage2 As String
EmailExtraMessage1 = Sheets("EMAILS").Range("EmailExtraMessage1")
EmailExtraMessage2 = Sheets("EMAILS").Range("EmailExtraMessage2")
EmailExtraMessage3 = Sheets("EMAILS").Range("EmailExtraMessage3")
EmailExtraMessage4 = Sheets("EMAILS").Range("EmailExtraMessage4")
Set thisWB = ThisWorkbook
shtname = "EMAILS"
EmailFrm = thisWB.Sheets("EMAILs").Range("D2")
selectedouc = Range("selectedframesouc")
seldate = Range("seldate")
selemail = Range("selframesemail")
FirstName = Range("FirstName")
'Create a new Microsoft Outlook session
Set appOutlook = CreateObject("outlook.application")
'create a new message
Set Message = appOutlook.CreateItem(olMailItem)
With Message
.Subject = "Exchange Performance: " & selectedouc & " - " & seldate & ""
.HTMLBody = "<span LANG=EN>" _
& "<p class=style2><span LANG=EN><font FACE=Calibri SIZE=3>" _
& "Hello " & FirstName & "," & "<br>" & EmailExtraMessage1 & "<br>" _
& "" & EmailExtraMessage2 _
& "<br>" _
& "" & EmailExtraMessage3 _
& "<br>" _
& "" & EmailExtraMessage4 _
& "<br>" _
& " <br >Click the attached link to download the full file " _
& "<a href=""[URL unfurl="true"]https://some[/URL] sharepoint address"">some report name</a></b>" _
Sheets("EMAILs").Select
Call createJpg(shtname, "J6:AB65", "summaryfile")
TempFilePath = Environ$("temp") & "\"
.Attachments.Add TempFilePath & "summaryfile.jpg", olByValue, 0
.HTMLBody = .HTMLBody & "<br>" _
& "<img src='cid:summaryfile.jpg'" & "width='880' height='300'><br>" _
& "<br>Regards,<br>Exchange Production Planning </font></span>"
.SentOnBehalfOfName = EmailFrm
.To = selemail
.Cc = ""
.Display
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Application.Calculation = xlCalculationAutomatic
End Sub
Code:
Sub createJpg(Namesheet As String, nameRange As String, nameFile As String)
ThisWorkbook.Activate
Application.Wait (Now + TimeValue("00:00:01"))
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 & ".jpg", "JPG"
End With
Worksheets(Namesheet).ChartObjects(Worksheets(Namesheet).ChartObjects.Count).Delete
Set Plage = Nothing
End Sub