+ Reply to Thread
Results 1 to 16 of 16

VBA code to send selected range in body of mail

  1. #1
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    VBA code to send selected range in body of mail

    Good day,

    please help me with arranging VBA code to send range of cells in body of mail message.

    I have bellow code, it is working but dont know how to display predefined range (A10:D50) in body of message. Also want to do that it will display only full cells.

    Sub SendMAIL()


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

    'Use hour to create a text
    Select Case Hour(Time)
    Case Is <= 12
    TxtHello = "Good Morning," & vbNewLine
    Case Is >= 12
    TxtHello = "Good Afternoom," & vbNewLine
    Case Is >= 18
    TxtHello = "Good Evening," & vbNewLine
    End Select



    'Create message
    On Error Resume Next
    With OutMail
    .to = "aaaa" 'Insert required address here ########
    .CC = "aaaa"
    .BCC = ""
    .Subject = "subject"
    .Body = TxtHello & "my message"

    .Attachments.Add strPath & strFName
    .Display 'Use only during debugging ##############################
    '.Send 'Uncomment to send e-mail ##############################
    End With

    'Delete any temp files created
    Kill strPath & strFName
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing


    End Sub

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA code to send selected range in body of mail

    Hi,

    This is an example of a procedure that I've used in the past. Based on an original from Ron de Bruin I think.

    I also have a more complex version that sends a range using SMTP. i.e. it doesn't need Outlook or indeed any email client be open. If that would be of any use post back.

    Not sure what you mean by 'only full cells'.

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: VBA code to send selected range in body of mail

    Dear,

    sorry but bellow code is not working for me.

    So i need a code that in body of message fill display content from range A10:D50.

    It is possible to add it in my code?

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA code to send selected range in body of mail

    Hi,

    Will you upload your workbook containing the code you have written after incorporating and editing the structure of the code I gave you.
    It will work, it's just that you will have made a mistake somewhere, hence the need to see what you have written in the context of your workbook.

  5. #5
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: VBA code to send selected range in body of mail

    find attached sample workbook...it is the same I will use in my real workbook.

    When clicking on button nothing happens.

    test.xlsm

  6. #6
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: VBA code to send selected range in body of mail

    Hi..

    Richard provided some nice code for you.. taking 3 seconds to step through it would have shown you that the error you made was not adding the correct Sheet name..

    Change this..
    Please Login or Register  to view this content.
    to this..
    Please Login or Register  to view this content.
    To step thought your code.. put a break point at the start.. and then use F8 to step thru... hover te cursor over variables to see there values as you go.. ( use Watch window also.. but maybe that come slater as you learn more)..

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA code to send selected range in body of mail

    Hi,

    You missed the most important point which I stressed in my email. You need to use the Sheets VBA Code Name. i.e.

    Please Login or Register  to view this content.
    Your sheet tab name "Mail" has the VBA code name of 'Sheet1'
    You could use
    Please Login or Register  to view this content.
    but it's generally regarded as good practice to use VBA code names since it's all too easy to change a sheet tab name which would cause the macro to fail. You'll also need to include a valid email name in the .To section.

  8. #8
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: VBA code to send selected range in body of mail

    Dears,

    MANY THANKS!...what a stupid error...

    Next question..is possible to display messsage prior to sending?

  9. #9
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: VBA code to send selected range in body of mail

    Change :
    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: VBA code to send selected range in body of mail

    already changet it..without any result

    test it: test.xlsm

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA code to send selected range in body of mail

    Hi,

    I don't believe the Mail_envelope method allows you to see the email before it's sent. The big advantage of this method is that it does not require Outlook to be open at the time. If you want the ability to review the email first then perhaps the slightly more complex procedure below.

    The disadvantage is that it is linked to an image of the range which has been saved as a picture in a folder that is identified in the code. If the image file is ever deleted then the link is not valid and the email won't show the picture.



    Please Login or Register  to view this content.

  12. #12
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: VBA code to send selected range in body of mail

    Yeap.. I got nothing.. seems Mail Envelope can NOT display before sends for the reasons Richard outlines..

    Richard.. is the method you describe above kinda like a shorter version of the RangetoHtml function (RonDeBruin)?

  13. #13
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: VBA code to send selected range in body of mail

    ok noted above, i will use your method Thanks!

    you are Excel Masters!

    my last question: hod to add text after these range? (so introduction is already in code..I want also add "conclusion"). it possible to define font size and style for whole message? so that it will the same..

  14. #14
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: VBA code to send selected range in body of mail

    Ders,

    please hel me with bellow code:

    Sub TEST()
    ' Create PDF of active sheet and send as attachment.

    Sheets("TEST").Select
    Dim strPath As String, strFName As String
    Dim OutApp As Object, OutMail As Object

    'Create PDF of active sheet only
    strPath = Environ$("temp") & "\" 'Or any other path, but include trailing "\"

    strFName = ActiveWorkbook.Name
    strFName = Worksheets("test").Range("A1").Value & " " & ActiveSheet.Name & ".pdf"

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    strPath & strFName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

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

    'Create message
    On Error Resume Next
    With OutMail
    .To = "" .CC = ""
    .BCC = ""
    .Subject = ""
    .Body = ""
    .Display 'Use only during debugging ##############################
    '.Send 'Uncomment to send e-mail ##############################
    End With

    'Delete any temp files created
    Kill strPath & strFName
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing




    End Sub
    It is possible to ammend these code that will generate PDF only from range and not entire sheet.

  15. #15
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: VBA code to send selected range in body of mail

    Have solved it.

  16. #16
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: VBA code to send selected range in body of mail

    hello,

    Maybe anyone knows how to made that bellow code will sedn mail to mail adresses noted in cell N2

    Sub Send_Selection_Or_ActiveSheet_with_MailEnvelope()



    Sheets("test").Select




    'Working in Excel 2002-2013
    Dim Sendrng As Range

    On Error GoTo StopMacro

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

    'Note: if the selection is one cell it will send the whole worksheet
    Sheets("test").Range("A1:E207 ").Select ' where MySheet is the VBA code name of the sheet containing the range in question."
    Set Sendrng = Selection

    'Create the mail and send it
    With Sendrng

    ActiveWorkbook.EnvelopeVisible = True
    With .Parent.MailEnvelope

    ' Set the optional introduction field thats adds
    ' some header text to the email body.


    With .Item
    .To = "[email protected]"
    .CC = ""
    .BCC = ""
    .Subject = "test"
    .send
    End With

    End With
    End With

    StopMacro:
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    ActiveWorkbook.EnvelopeVisible = False




    End Sub

  17. #17
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: VBA code to send selected range in body of mail

    Good day,

    is possible to ammend bellow code that will send mail to adresses noted in cell N2

    Sub Send_Selection_Or_ActiveSheet_with_MailEnvelope()



    Sheets("test").Select




    'Working in Excel 2002-2013
    Dim Sendrng As Range

    On Error GoTo StopMacro

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

    'Note: if the selection is one cell it will send the whole worksheet
    Sheets("test").Range("A1:E207 ").Select ' where MySheet is the VBA code name of the sheet containing the range in question."
    Set Sendrng = Selection

    'Create the mail and send it
    With Sendrng

    ActiveWorkbook.EnvelopeVisible = True
    With .Parent.MailEnvelope

    ' Set the optional introduction field thats adds
    ' some header text to the email body.
    '.Introduction = TxtHello


    With .Item
    .To = "[email protected]"
    .CC = ""
    .BCC = ""
    .Subject = "test"
    .send
    End With

    End With
    End With

    StopMacro:
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    ActiveWorkbook.EnvelopeVisible = False




    End Sub

  18. #18
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: VBA code to send selected range in body of mail

    It is possible to ammend above code that will select mail adresses from cell N2?
    Last edited by forfiett; 07-17-2014 at 07:28 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. send email through excel with different cell range in the body of the mail
    By melody10 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-18-2013, 12:41 AM
  2. Send e-mail with image on body (.html) on e-mail manager <> outlook
    By mariotnc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-05-2012, 09:28 PM
  3. Word Format as Body of mail using Send mail
    By andy_iyeng in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-19-2012, 05:04 AM
  4. Replies: 0
    Last Post: 09-15-2011, 08:24 PM
  5. Send E-mail with image in e-mail body
    By ncaravela in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-27-2010, 10:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1