Results 1 to 6 of 6

Multiple lines in body of VBA generated email

This is a discussion on Multiple lines in body of VBA generated email within the Excel Questions forums, part of the Question Forums category; I'm attempting to put multiple lines of text (not one huge string) in the body of an email. I've tried ...

  1. #1
    New Member
    Join Date
    Dec 2010
    Posts
    32

    Default Multiple lines in body of VBA generated email

    I'm attempting to put multiple lines of text (not one huge string) in the body of an email. I've tried Chr(13), Chr(10), vbcr, and vblf....all to no avail.

    Sub AttachEmail()

    Bk1 = ActiveWorkbook.Name
    Sh1 = ActiveSheet.Name

    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
    .To = "$ES&T MCV"
    .CC = ""
    .Subject = "MCV Schedule"
    .Attachments.Add ""
    .HTMLBody = "First line here" & Chr(10) & "Second line here"
    .Display
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
    End Sub

    Thanks

  2. #2
    New Member warhaug's Avatar
    Join Date
    Jan 2012
    Posts
    26

    Default Re: Multiple lines in body of VBA generated email

    Try changing to
    Code:
    Sub AttachEmail()
    
     Bk1 = ActiveWorkbook.Name
     Sh1 = ActiveSheet.Name
    
     Dim OutApp As Object
     Dim OutMail As Object
    
     Dim strbody As String 
     
    Set OutApp = CreateObject("Outlook.Application")
     Set OutMail = OutApp.CreateItem(0)
     
    strbody = "Hi there" & vbNewLine & vbNewLine & _
                  "This is line 1" & vbNewLine & _
                  "This is line 2" & vbNewLine & _
                  "This is line 3" & vbNewLine & _
                  "This is line 4"
    
    On Error Resume Next
     With OutMail
     .To = "$ES&T MCV"
     .CC = ""
     .Subject = "MCV Schedule"
     .Attachments.Add ""
     .HTMLBody = strbody
     .Display
     End With
     On Error GoTo 0
     Set OutMail = Nothing
     Set OutApp = Nothing
     End Sub

  3. #3
    Board Regular JP2112's Avatar
    Join Date
    Oct 2008
    Location
    NYC
    Posts
    237

    Default Re: Multiple lines in body of VBA generated email

    Please use [code][/code] tags when posting code.

    The HTMLBody Property requires HTML/CSS styling. So it would be something like


    "

    First line here

    Second line here

    "
    Last edited by JP2112; Jun 6th, 2012 at 02:55 PM. Reason: code mangled
    Regards,
    JP

    - Posting guidelines, forum rules and terms of use
    - Use [code][/code] tags when posting code
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes
    - My website: JP SoftTech

  4. #4
    Board Regular JP2112's Avatar
    Join Date
    Oct 2008
    Location
    NYC
    Posts
    237

    Default Re: Multiple lines in body of VBA generated email

    OK so my code is getting mangled by the editor. You need to wrap each line in p tags to denote separate paragraphs for each line. Hope that helps.
    Regards,
    JP

    - Posting guidelines, forum rules and terms of use
    - Use [code][/code] tags when posting code
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes
    - My website: JP SoftTech

  5. #5
    New Member
    Join Date
    Dec 2010
    Posts
    32

    Default Re: Multiple lines in body of VBA generated email

    Thanks JP, worked great.

  6. #6
    New Member
    Join Date
    Jun 2013
    Posts
    6

    Default Re: Multiple lines in body of VBA generated email

    Dear Jp

    i need ur help actually i required separate paragraphs in of each line . VBA Code pasted below E.i like

    Dear Sir , ( in bold font )

    1) Please .......
    2) Thanks & Regars
    3) Artemis

    VBA Code here and Excel file i used
    Code Name Emailid
    1350 Rajinder Prasad Rajinder@artemishealthsciences.com


    Sub Mail_Ap()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet

    Dim strbody As String



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

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("C").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "?*@?*.?*" And _
    Application.WorksheetFunction.CountA(rng) > 0 Then
    Set OutMail = OutApp.CreateItem(0)
    strbody = "Dear " & cell.Offset(0, -1).Value & vbNewLine & vbNewLine & _
    "Please" & vbNewLine & _
    "thanx" & vbNewLine & _
    "kapil" & vbNewLine & _
    "artemis Hospital"

    On Error Resume Next



    With OutMail
    .to = cell.Value
    .CC = "viveka@artemishealthsciences.com"
    .Subject = "Account Statment"
    .HTMLBody = strbody




    .Display

    End With
    On Error GoTo 0


    Set OutMail = Nothing
    End If
    Next cell

    Set OutApp = Nothing
    With Application
    .EnableEvents = True
    .ScreenUpdating = True
    End With
    End Sub
    Last edited by rajinder; Aug 7th, 2013 at 09:51 AM.

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com