Sending Email with Table in Body

lharr28

New Member
Joined
May 22, 2024
Messages
25
Office Version
  1. 365
Platform
  1. Windows
I'm trying to send an email that includes a table from my excel file in the body of the email. I came across a rangetohtml function by Ron de Bruin, but I cannot get my email to display the table. The email shows where I tried to insert the table. All I did was copy and paste the VBA directly into one of the modules. I created the sub in module 1 and the functiontohtml in module 2. I've include the code below and some screenshots of the data.

1724947735761.png


VBA Code:
Dim EApp As Object
        Set EApp = CreateObject("Outlook.Application")

        Dim EItem As Object
        Set EItem = EApp.CreateItem(0)
 
 '**NEW STUFF ADDED 8/29
        'declare varialbes: rows counts down and col counts across
        Dim count_row, count_col As Integer
        Dim revexp As Range    'table on that sheet based on the count row and count col variables
    
 '**NEW STUFF ADDED 8/29
        'row and column counts
        count_row = WorksheetFunction.CountA(Range("A14", Range("A14").End(xlDown)))
        count_col = WorksheetFunction.CountA(Range("A14", Range("A14").End(xlToRight)))
      
  '**NEW STUFF ADDED 8/29
        'set the table range for revexp sheet
        Set revexp = Sheets("RevExp").Range(Cells(14, 14), Cells(count_row, count_col))
        
    '****Display email and specify To, Subject, Body etc
        With EItem
        
            deptname = Range("J25")
            
            .SentOnBehalfOfName = "abc@gmail.com"
            .To = Range("J22")
            .BCC = Range("J23")
            .Subject = "FY24" & " " & "Budget Letter" & " " & "-" & " " & deptname & " " & "(" & Format(Date, "mm-d-yy") & ")"
            
            'To break a single stmt into multiple lines use the underscore immediately preceded by a space and immediately followed by a line terminator like in the example below
            .HTMLbody = "Dear Budget Holder,<br/><br/>We hope this email finds you well and in high spirits.  We are pleased to inform you that the Team has received approved" _
            & "  budgets for <b>FY 2023 - 2024</b>.  Below you will find your department's budget information for FY24." _
            & " & RangetoHTML(revexp) & .HTMLbody" _
            & "<br/><br/> <font color = red><b>Please review the attached budget letter as it includes vital information and key points pertaining to your department's budget." _
            & "</font color></b><br/><br/>Warm regards,<br><img src='C:\Users\bbob11\OneDrive - Green Pages\Pictures\ABC Signature.jpg' 'height=200 width=300>" _

            .Display
            .Attachments.Add PDFFile
            'Add CLFSC signature as an attachment and to hide the image attachment (set the position argument to O)
            .Attachments.Add "C:\Users\bbob11\OneDrive - Green Pages\Pictures\ABC Signature.jpg", 1, 0
            .Display
                
            If DisplayEmail = False Then
            
                .Send
                
            End If
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
VBA Code:
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    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
    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

    '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=")

    '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
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try replacing this line...

VBA Code:
& " & RangetoHTML(revexp) & .HTMLbody" _

with

VBA Code:
& RangetoHTML(revexp) & .HTMLbody _

Hope this helps!
 
Upvote 1

Forum statistics

Threads
1,223,702
Messages
6,173,961
Members
452,539
Latest member
delvey

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