Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15151

Email Sheet Range With Images/Formatting as Email Body

$
0
0
Hey guys, I hope you are all going well!

I am trying to email a sheet range as the emails body, the existing code works almost perfectly however, hyperlinks, images and certain text formatting like: Italics, are not being copied onto the temp workbook and then ultimately the email body. Would anyone be able to provide any assistance?

* Author of source code: Ron de Bruin from Excel Automation. All credit goes to Ron for his fantastic code.
Many thanks in advance!! P.S. I'm Completely new to VBA

Office 365,VBA Version: 7.1 Excel Version: 2007, Build: 13209, Year: Current (2020)


Code:

Function RangetoHTML(rng As Range)
'Auther Ron de Bruin From Excel Automation

Dim email1 As Worksheet
    Set email1 = ActiveWorkbook.Worksheets("email")
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 Paste:=-4104
        .Cells(1).PasteSpecial Paste:=13
        .Cells(1).PasteSpecial Paste:=-4122
        .Cells(1).PasteSpecial Paste:=12
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        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=")
    TempWB.Close savechanges:=False
    Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing

End Function


Sub Mail_Range()

Dim email1 As Worksheet
    Set email1 = ActiveWorkbook.Worksheets("email")
Dim rng As Range
    Set rng = Nothing
    Set rng = email1.Range("B1:L37")
    Set rng = Sheets("email").Range("B1:L37").SpecialCells(xlCellTypeVisible)
Dim OutApp As Object
    Set OutApp = CreateObject("Outlook.Application")
Dim OutMail As Object
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    On Error GoTo 0
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    On Error Resume Next
    With OutMail
        .To = "Test"
        .CC = ""
        .BCC = ""
        .Subject = "Test"
        .HTMLBody = 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


Viewing all articles
Browse latest Browse all 15151

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>