0

i have this code to paste a range into an email but as text, is there a way i can modify it so it can paste the range as picture? The reason why I need it is because some cells that I want to paste have data bars and these do not display in the email, if you have a solution to this it will also help Thanks in advance

Sub SaveImage()

Dim tmp As Variant, str As String, h As Double, w As Double

Dim Rng As Range
Set Rng = Nothing
Set Rng = ThisWorkbook.Worksheets("Week Effectivity").Range("A1:M15").SpecialCells(xlCellTypeVisible)


Dim OA, OM As Object
Set OA = CreateObject("Outlook.Application")
Set OM = OA.CreateItem(0)

With OM
.To = "email"
'.CC = "email"
.Subject = "por ahi va"
 
 .HTMLBody = RangetoHTML(Rng)
 .Pictures.Paste
.Send

End With
Set OM = Nothing
Set OA = Nothing

Application.PrintCommunication = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.StatusBar = False

End Sub


Function RangetoHTML(Rng As Range)
    Dim FSO As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
   ' PDF_FILE = ""
    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".html"
  
    ' Copy the range and create a workbook to receive the data.
    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).PasteSpecial xlPasteAllMergingConditionalFormats, , False, False
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        On Error GoTo 0
    End With
  ' Publish the sheet to an .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 the RangetoHTML subroutine.
    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.
    Kill TempFile
    
    Set ts = Nothing
    Set FSO = Nothing
    Set TempWB = Nothing
    Application.CutCopyMode = True
End Function
BigBen
  • 38,994
  • 6
  • 24
  • 37
  • 1
    Well-covered here: https://stackoverflow.com/questions/29092999/pasting-an-excel-range-into-an-email-as-a-picture – Tim Williams Oct 21 '21 at 16:14

0 Answers0