• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

email selection cells,format and put in body

jainacute

New Member
Hi Im new here, I ended up being here trying to research codes.

I appreciate if someone can help me.

I have data like this

A B C D E F G(these are columns)
1 John Boy US 20 Staff 5 Apple
2 Hannah Girl IND 25 mgr 15 Orange
3 Jason Boy JP 23 Staff 5 Banana
4 Hannah Girl KOr 21 mgr 15 Orange

Email body to look like this

if i select John
(Column A & B)
Name John
Gender Boy
Age 20
Fruit Apple

if i select John & Rachel
(Column A & B)
Name John
Gender Boy
Age 20
Fruit Apple

Name Rachel
Gender Girl
Age 21
Fruit Lemon


Thanks in advance
 
Copying/Modifying main code from Ron's site.

Code:
Sub Mail_Range()
'Working in Excel 2000-2013
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim Source As Range
    Dim c As Range
    Dim OutBody As String
    Dim OutApp As Object
    Dim OutMail As Object
   

    Set Source = Nothing
    On Error Resume Next
    Set Source = Selection
    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
'Build the message
    For Each c In Source
        OutBody = OutBody & "Name " & c.Value & Chr(10)
        OutBody = OutBody & "Gender " & c.Cells(1, 2).Value & Chr(10)
        OutBody = OutBody & "Age " & c.Cells(1, 4).Value & Chr(10)
        OutBody = OutBody & "Fruit " & c.Cells(1, 7).Value & Chr(10)
        'Add a space line
        OutBody = OutBody & Chr(10)
    Next c

    On Error Resume Next
    With OutMail
        .to = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = OutBody
        '.Send   'or use .Display
        .display 'Current set just to display. Comment out whichever line you want
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 
thank you very much it was fast reply,, it gave me an idea.,

Is it possible that we can do it in tempfile? (which will help me to edit the cells)

because my boss wants the interior color of the cell to be blue and skyblue alternately and the text to be bold and put borders every cell selection that will put in the body of email outlook.

Im hoping you can still help me.. Thanks and advance.
 
I'm not sure what you mean. There are no "cells" in the body of an email message. Are you wanting to send an attachment in the email?
 
Im thinking of htmlbody. Putting the selection range to temporary file then killing it, and copy it to the body of outlook.
With this I can copy even the format done in the temp file.
Is it possible with this scenario?
 
Ah, got it now. Yes, we'll just use the Function that Ron had on his page to build the HTM table. Here's the whole code (macro + function).
Code:
Sub Mail_Range()
'Working in Excel 2000-2013
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
   Dim Source As Range
    Dim OutBody As String
    Dim OutApp As Object
    Dim OutMail As Object
   

    Set Source = Nothing
    On Error Resume Next
    Set Source = Selection
    On Error GoTo 0

    If Source Is Nothing Then
        MsgBox "The source is not a range or the sheet is protected, please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    On Error Resume Next
    With OutMail
        .to = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(Source)
        '.Send   'or use .Display
       .display 'Current set just to display. Comment out whichever line you want
   End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim i As Long
    Dim c As Range
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        'Build the message
        i = 0
        For Each c In rng
            .Cells(1 + (4 * i), 1) = "Name"
            .Cells(1 + (4 * i), 2) = c.Value
            .Cells(2 + (4 * i), 1) = "Gender"
            .Cells(2 + (4 * i), 2) = c.Cells(1, 2).Value
            .Cells(3 + (4 * i), 1) = "Age"
            .Cells(3 + (4 * i), 2) = c.Cells(1, 4).Value
            .Cells(4 + (4 * i), 1) = "Fruit"
            .Cells(4 + (4 * i), 2) = c.Cells(1, 7).Value
            i = i + 1
        Next c
    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
 
Back
Top