VBA - Send Email including Range (Pasted as Picture) + Pivot table below


New Member
Nov 16, 2017
I was hoping that somebody could help me.

I am working on a spreadsheet ("Email") including a first block (Range B2:G10)(used as introduction, with logo, pictures,..) . Below the block of introduction is located a Pivottable ("PivotAddress1"). The Pivot table starts on line B13:G13.

I could find/modify the code in order to get the pivot table sent by email and would like now to add the block of introduction on top of the Pivot. ideally, I would like to get the block of intro (Range B2:G10) pasted as a picture, in order to keep the pictures, logos, displayed as they are on the spreadsheet.

Here is the code that I have for the Pivot, for some reason, I can't seem to find the trick to include the picture of the block of intro on top:

(As you will notice, I called twice the RangeToHTML function as I was capturing the block of intro already but the formatting + pictures are not carried so would like to switch to the pasting as a picture solution (Just for the Block), the Pivottable should remain the way it is.

Sub Mail_Selection_Range_Outlook_Body()

'Working in Excel 2000-2016
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object


    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    Set PT = ActiveSheet.PivotTables(1) 'K: Will select the Pivot Table
    PT.TableRange1.Select               'K: Will select the Pivot Table
    'Set rng = Selection.SpecialCells(xlCellTypeVisible)
    Set rng = Sheets("Email").Range("B2:G10")
    rng2 = RangetoHTML(rng)
    Set rng = Sheets("Email").PivotTables("PivotAddress1").TableRange1
    rng3 = RangetoHTML(rng)
    'You can also use a fixed range if you want
    'Set rng = Sheets("YourSheet").Range("D4:D12").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 = "xx@xx.com"
        .CC = ""
        .BCC = ""
        .Subject = "Sddress updates Date: " & Format(Now, "YYYY/MM/DD")
        .HTMLBody = rng2 & "
" & rng3
        .Display   'or use .Send
    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
    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
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        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, _
        .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
    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
Thank you for any help,

Some videos you may like

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...