0

I have Excel code to generate a response to a selected email. The response includes text which is dynamic, based on processing done in Excel.

I want the response to include the original email text. I tried some things but none preserve the original email formatting, making it appear distorted, e.g. tables being broken up.

Sub Send_Email()
    Dim emailApplication As Object
    Dim emailItem As Object

    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.ActiveExplorer.Selection.Item(1).ReplyAll

    emailItem.SentOnBehalfOfName = "dummy@dummy.com"

    emailItem.Body = Sheets("Email").Range("A34")

    emailItem.Display

    Set emailItem = Nothing
    Set emailApplication = Nothing
End Sub
ZygD
  • 22,092
  • 39
  • 79
  • 102
  • Check this https://www.datanumen.com/blogs/auto-reply-original-email-predefined-text-via-outlook-vba/ – Ricardo Diaz Jan 07 '21 at 10:59
  • Read up on `.HTMLBody` rather than `.Body` - there a number of ways to do it depending on your need. Think of Body as just plain text, whereas HTMLBody allows for HTML tags which cover the type of format you require. A lot of examples and similar questions. I'd suggest having a go and coming back with another question if you get stuck. – redditor Jan 07 '21 at 11:10
  • Possible duplicate of [Paste Excel range in Outlook](https://stackoverflow.com/questions/18663127/paste-excel-range-in-outlook) – niton Jan 08 '21 at 17:34

1 Answers1

0

Try this

With emailItem.ReplyAll
    .HTMLBody = "<p>" & Sheets("Email").Range("A34").Value & "</p><br>" & .HTMLBody

    .Display
End With

If Sheets("Email").Range("A34").Value has formatting that you need to preserve, then you will have to convert that also into html text using RangetoHTML(rng). I have added the relevant code below in case that link dies.

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
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • There is an error where emailItem.ReplyAll should be just emailItem. Nevertheless, this worked a treat. I was able to convert the text in the range to HTML format by editing in place in Excel. I was actually using CHAR(10) to take line breaks, so I just replaced this with
    or

    as necessary and everything is working fine now.

    – dfay1992 Jan 07 '21 at 13:24