2013 Excel VBA Email as bitmap

mrjnice85

New Member
Joined
May 12, 2015
Messages
9
Good Afternoon,

I am trying to use VBA to copy the visible cells into an email and insert that information into the body of the email as a bitmap. The code I am using currently is great for just pasting the data into the email but I need it converted to bitmap as some of the email recipients are using blackberry and bitmap is a lot easier to read then. I'm no code guru so i have pieced it together from the various corners of the internet. I am sure it is something simple I am missing. I have tried changing the file extension in the first section of code from .htm to .bmp with no success.


Code:
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"
 
    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
 
    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
 
    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=")
 
    TempWB.Close savechanges:=False
 
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function


Code:
Sub AgentScoreCard_EmailMTD()


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


    Set rng = Nothing
    On Error Resume Next
    Set rng = Sheets("AgentScoreCard").Range("E3:U56").SpecialCells(xlCellTypeVisible)
    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 = [A1]
        .CC = [A2]
        .BCC = ""
        .Subject = Date & " Month to Date Stats"
        .HTMLBody = "Hello Team,

" & "Below are the Month to Date Stats." & RangetoHTML(rng)
        .Display
    End With
    On Error GoTo 0


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


    Set OutMail = Nothing
    Set OutApp = Nothing


End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Thanks for the reply RickXL. I took a look at the post and modified it the best I could to my sheet; however, when I run it I get this error:

Run-time error '9': Subscript out of range

When I run the debug it highlights
tstamp= Sheets("Save and Send").Range("D5")


I know I don't have that sheet in my workbook and from what I understand of the rest of the formula nothing in the code applies anything to this area. Granted my coding "knowledge" is limited so i'm probably overlooking something. Current code being used below:

Code:
Sub emailCARE()
    
    '===================================================
    ' Export Range as PNG file
    '===================================================
    ' Set Range you want to export to file
    Dim r As Range
    Dim co As ChartObject
    Dim picFile As String
    Set r = Worksheets("Snapshot").Range("C3:I23")
    
    ' Copy range as picture onto Clipboard
    r.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
    picFile = Environ("Temp") & "\TempExportChart.png"
    
    ' Create an empty chart with exact size of range copied
    Set co = r.Parent.ChartObjects.Add(Left:=r.Left, Top:=r.Top, Width:=r.Width, Height:=r.Height)
    With co
        ' Paste into chart area, export to file, delete chart.
        .Chart.Paste
        .Chart.Export picFile
        .Delete
    End With
    


    '===================================================
    ' Create Email and Import Picture
    '===================================================
    ' Send out the email
    Dim OutApp As Object
    Set OutApp = CreateObject("Outlook.Application")
    Dim OutMail As Object
    Set OutMail = OutApp.CreateItem(olMailItem)
    
    Dim signature As String
    Dim tstamp As String
    Dim strBody As String
    
    ' Subject location
    tstamp = Sheets("Save and Send").Range("D5")
    OutMail.Display
    signature = OutMail.HTMLBody
    
    ' Change change email list here
    strBody = "<body> <h2>Report</h2> <img src=""" & picFile & """ style=""width:304px;height:228px""></body>"
    
    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = tstamp
        .HTMLBody = strBody & vbNewLine & signature
        .Attachments.Add Sheets("Save and Send").Range("D4") & Sheets("Save and Send").Range("D23") ' attaching the pdf
    End With
    Kill picFile
    On Error GoTo 0
    
    'Tidy Up
    Set OutMail = Nothing
    Set OutApp = Nothing
    Set co = Nothing
    Set r = Nothing


End Sub
 
Upvote 0
Hi,

You are quite right. The error means you do not have that sheet.

The variable tstamp just supplies the Subject to the email. In my original version it read that from a worksheet. Just make the Subject whatever you want.

EDIT: I just noticed that the macro added a pdf file as well. The name was also in that worksheet. You should omit the:
Code:
.Attachments.Add ...
line as well.

Regards,
 
Last edited:
Upvote 0
Hi,

No problem, I am pleased you got it to work for you.

Thanks for letting me know.

regards,
 
Upvote 0
So something interesting is coming up. The code seems to work and generate the email with the range pasted in the body as an image for review and you have to manually click the send in the email just so it gives you a chance to review or add to the email; however, sometimes when you hit send it sends a email with a blank body. It doesn't send the image for whatever reason. Is there something in the code that causes this? Maybe the Kill picFile line needs to be further down in the code or removed?

Code:
Sub emailCARE()
    
    '===================================================
    ' Export Range as PNG file
    '===================================================
    ' Set Range you want to export to file
    Dim r As Range
    Dim co As ChartObject
    Dim picFile As String
    Set r = Worksheets("Snapshot").Range("C3:I23")
    
    ' Copy range as picture onto Clipboard
    r.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
    picFile = Environ("Temp") & "\TempExportChart.png"
    
    ' Create an empty chart with exact size of range copied
    Set co = r.Parent.ChartObjects.Add(Left:=r.Left, Top:=r.Top, Width:=r.Width, Height:=r.Height)
    With co
        ' Paste into chart area, export to file, delete chart.
        .Chart.Paste
        .Chart.Export picFile
        .Delete
    End With
    


    '===================================================
    ' Create Email and Import Picture
    '===================================================
    ' Send out the email
    Dim OutApp As Object
    Set OutApp = CreateObject("Outlook.Application")
    Dim OutMail As Object
    Set OutMail = OutApp.CreateItem(olMailItem)
    
    Dim signature As String
    Dim tstamp As String
    Dim strBody As String
    
    ' Subject location
    OutMail.Display
    signature = OutMail.HTMLBody
    
    ' Change change email list here
    strBody = "<body> <img src=""" & picFile & """ style=""width:304px;height:228px""></body>"
    
    On Error Resume Next
    With OutMail
        .To = "Example@Example.com"
        .CC = "Example@Example.com"
        .BCC = ""
        .Subject = "CARE Intra-Day Snapshot " & Date
        .HTMLBody = strBody & vbNewLine & signature
    End With
    Kill picFile
    On Error GoTo 0
    
    'Tidy Up
    Set OutMail = Nothing
    Set OutApp = Nothing
    Set co = Nothing
    Set r = Nothing


End Sub
 
Upvote 0
Hi,

If the image appears in the email and is not blank then it should be sent correctly.

My original code had the following line prior to the copy statement at the start of the macro:
Code:
Application.ScreenUpdating = True ' Required for CopyPicture Method

That makes sure that the copy of the range to the chart works properly. If that does not work properly then it will export a blank file. That is because if ScreenUpdating is False then the Paste does not happen straightaway and you just get a blank chart exported.

I think the Kill command is in a good place.

If you single step through the code using F8 you should be able to see that a blank frame appears in the spreadsheet when it creates an empty chart.
Then it should be filled with the image following the .Chart.Paste command.
After the .Chart.Export command you should be able to go to your Temp folder and check that the file has an image in it.
As long as the image appears in the email preview I don't see how it could get lost.

I hope this helps.
 
Upvote 0
It's strange... the step through seems fine. I never see a file popup in my temp folder though. It puts the picture in the email but sometimes when you click send in the email it sends an empty email even though when you hit send the photo was in there. It never seems to be an issue on my computer but when I have my co-workers use it the first time it sends fine but any subsequent attempt it does this blank email.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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