Ankan Bhattacharjee
Ankan Bhattacharjee

Reputation: 21

Paste Excel range to Outlook email body

I want to copy data from a fixed range in an Excel sheet and paste on an email body.

Below is the code I have come up with. However I am not able to paste the specified range A11:H12.

Private Sub CommandButton1_Click()
    On Error GoTo ErrHandler

' SET Outlook APPLICATION OBJECT.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")

' CREATE EMAIL OBJECT.
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
    .To = "email"
    .Subject = "test"
    .Body = ActiveSheet.Range("A11:H12").Select
    .Display        ' DISPLAY MESSAGE.
End With

' CLEAR.
Set objEmail = Nothing:    Set objOutlook = Nothing

ErrHandler:
    '
End Sub

Upvotes: 2

Views: 26164

Answers (2)

krib
krib

Reputation: 569

As in the comments Ron has this figured. The below code and function does the trick. Copy them both.

Private Sub CommandButton1_Click()

' SET Outlook APPLICATION OBJECT.
Dim rng As Range
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")

Set rng = Nothing
On Error Resume Next
Set rng = ActiveSheet.Range("A11:H12").SpecialCells(xlCellTypeVisible)
On Error GoTo 0

' CREATE EMAIL OBJECT.
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = "email"
.Subject = "test"
.HTMLBody = RangetoHTML(rng)
.Display        ' DISPLAY MESSAGE.
End With

' CLEAR.
Set objEmail = Nothing:
Set objOutlook = Nothing

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

Upvotes: 2

Tiago C N Viana
Tiago C N Viana

Reputation: 41

Code:

    Sub sendEmail()
    'call outlook
    Dim MyOlapp As Object, MyItem As Object
    Set MyOlapp = CreateObject("Outlook.Application")
    Set MyItem = MyOlapp.CreateItem(olMailItem)
        'ajust range of sheet
        Range("A11:H12").Select
        Selection.Copy

    With MyItem
        'ajust number of sheet
        .To = Sheet17.[b1].Value 'e-mail adress
        .Subject = Sheet17.[b2].Value 'subject of e-mail
        .Body = Sheet17.[b3].Value 'body of e-mail
        .Display
        SendKeys ("^{DOWN}")
        SendKeys ("^{DOWN}")
        SendKeys ("%m")
        SendKeys ("v")
        SendKeys ("s")
        SendKeys ("{UP}")
        SendKeys ("{UP}")
        SendKeys ("{ENTER}")
        SendKeys ("{ENTER}")
        SendKeys ("%m")
        SendKeys ("q")
        SendKeys ("{ENTER}")


    End With
    End Sub

Upvotes: 0

Related Questions