0

I'm trying to copy and paste a range of cells to Outlook.

What I'm trying to accomplish:
End Result Potential

Sub Send_Email_Condition_Cell_Value_Change()
    Dim pApp As Object
    Dim pMail As Object
    Dim pBody As String
    Dim rng As Range
    Set rng = Range("B6:C16")
    Set pApp = CreateObject("Outlook.Application")
    Set pMail = pApp.CreateItem(0)
    On Error Resume Next
    With pMail
        .To = "@gmail.com"
        .CC = ""
        .BCC = ""
        .Subject = "NAME Account Action Price Notification"
        .Body = "Hello, our recommended action price for NAME of PRICE has been hit." & vbNewLine & vbNewLine & _
            "Thank you."
        .Display
         Dim wdDoc As Object     '## Word.Document
         Dim wdRange As Object   '## Word.Range
        Set wdDoc = OutMail.GetInspector.WordEditor
        Set wdRange = wdDoc.Range(0, 0)
        wdRange.InsertAfter vbCrLf & vbCrLf
        'Copy the range in-place
        rng.Copy
        wdRange.Paste
    End With
    On Error GoTo 0
    Set pMail = Nothing
    Set pApp = Nothing
End Sub

This code opens the email and populates all of the text needed, but I cannot get it to copy and paste the cells.

Community
  • 1
  • 1
HunterN
  • 5
  • 2
  • Perhaps the following might help a bit. They seem to use .PasteAndFormat instead of .Paste. (I suppose it's just Outlook being annoying, as usual) https://stackoverflow.com/a/48897463/3654325 – Stax Dec 08 '22 at 22:36

1 Answers1

0

try (with range to html function)

Sub table_to_email()
   Dim mytable As Range
   Dim str1 As String
   Dim str2 As String


   Set mytable = Sheets("Sheet1").Range("B6:C16")

   str1 = "Hello, our recommended action price for NAME of PRICE has been hit."
   str2 = "<br><br> Thank you, <br> Your name"

   With CreateObject("outlook.application").CreateItem(0)
        .To = "someone@somewhere.com"
        .Subject = "NAME Account Action Price Notification"
        '.Body = ""
        '.Attachments.Add
        .Display '.Send
        .HTMLBody = str1 & RangetoHTML(mytable) & str2 & .HTMLBody
   End With

   End Sub


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
k1dr0ck
  • 1,043
  • 4
  • 13