2
votes

I'm trying to create a custom email when button is clicked in Excel. All users have Outlook. In the body of the email I want to include a portion of a spreadsheet that is already formatted.

I can get the information in there but I can't get the order right in the body. That is the text, then formatted table THEN signature.

The example below puts the formatted table BELOW but I want the signature to be the last thing.

Any help would be greatly appreciated.

Sub SendUpdateEmail()

Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Dim EmailTo As String
Dim EmailCC As String
Dim UpdateDate As String
Dim Location As String
Dim strSig As String


' Set Outlook object
Set outlook = CreateObject("Outlook.Application")

' Set Email Mail Object
Set newEmail = outlook.CreateItem(0)

' Set Inspect Object
Set xInspect = newEmail.GetInspector

' Set Page Editor Object
Set pageEditor = xInspect.WordEditor

' Set Email To
EmailTo = Worksheets("Project Summary").Cells(15, "F").Value

' Set Email CC
EmailCC = Worksheets("Project Summary").Cells(16, "F").Value

' Set Update date
UpdateDate = Worksheets("OUTPUT - Daily Field Ticket").Cells(7, "B").Value

' Set Location
Location = Worksheets("OUTPUT - Daily Field Ticket").Cells(5, "B").Value



With newEmail
.To = EmailTo
.CC = EmailCC
.BCC = ""
.Subject = "UPDATE | " + Location + " | " + UpdateDate

'DO NOT REMOVE - THIS MUST BE VISIBLE FIRST TO GET THE DEFAULT SIGNATURE
.Display

'GET THE HTML CODE FROM THE SIGNATURE
strSig = .HTMLBody

.HTMLBody = "Hello," & "<br>" & "<br>" & "Please see attached the Daily Field Ticket for " + Location 
+ " for " + UpdateDate + "." + strSig


Sheet1.Range("A28:F35").Copy

pageEditor.Application.Selection.Start = Len(.Body)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)

Set pageEditor = Nothing
Set xInspect = Nothing

End With

Set newEmail = Nothing
Set outlook = Nothing

End Sub
2

2 Answers

0
votes

When I have to copy paste ranges in Outlook mails I usually use the famous function "Range to HTML" by Ron De Bruin. I've inserted it in your code and edit it a little. It should give you the expected result:

Sub SendUpdateEmail()

Dim outlook As Object
Dim newEmail As Object
Dim EmailTo As String
Dim EmailCC As String
Dim UpdateDate As String
Dim Location As String
Dim strSig As String


' Set Outlook object
Set outlook = CreateObject("Outlook.Application")
' Set Email Mail Object
Set newEmail = outlook.CreateItem(0)
' Set Email To
EmailTo = "[email protected]"
' Set Email CC
EmailCC = "[email protected]"
' Set Update date
UpdateDate = "18/03/2020"
' Set Location
Location = "Here"

With newEmail
.To = EmailTo
.CC = EmailCC
.BCC = ""
.Subject = "UPDATE | " + Location + " | " + UpdateDate
'DO NOT REMOVE - THIS MUST BE VISIBLE FIRST TO GET THE DEFAULT SIGNATURE
.Display
'GET THE HTML CODE FROM THE SIGNATURE
strSig = .HTMLBody

.HTMLBody = "Hello," & "<br>" & "<br>" & "Please see attached the Daily Field Ticket for " + Location + _
" for " + UpdateDate + "." + RangetoHTML(Sheet1.Range("A28:F35")) & "<br>" & strSig

.Display

End With

Set newEmail = Nothing
Set outlook = Nothing

End Sub


Function RangetoHTML(rng As Range)
' By Ron de Bruin.
    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

0
votes

Remove the .HTMLBody and work with Page Editor Set pageEditor = xInspect.WordEditor


Example

With newEmail
   .To = EmailTo
   .CC = EmailCC
   .BCC = ""
   .Subject = "UPDATE | " + Location + " | " + UpdateDate

   'DO NOT REMOVE - THIS MUST BE VISIBLE FIRST TO GET THE DEFAULT SIGNATURE
   .Display

    Worksheets("Sheet1").Range("A28:F35").Copy

    pageEditor.Paragraphs(1).Range.PasteAndFormat (wdFormatPlainText) & vbCr & vbLf

    pageEditor.Range.InsertBefore "Hello," & vbCr & _
                            "Please see attached the Daily Field Ticket for " _
                            + Location + " for " + UpdateDate + "." & vbCr & vbCr



End With