Multiple lines in body of VBA generated email

huffduf41

New Member
Joined
Dec 3, 2010
Messages
39
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
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
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
 
Upvote 0
Please use [code][/code] tags when posting code.

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


HTML:
<pre>"<p>First line here</p><p>Second line here</p>"</pre>
 
Last edited by a moderator:
Upvote 0
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.
 
Upvote 0
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
NameEmailid
1350Rajinder PrasadRajinder@artemishealthsciences.com

<colgroup><col><col><col></colgroup><tbody>
</tbody>


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:
Upvote 0
I'm stuck here and I can't enter a line, here is my code:

Sub Email()

Dim r As Range
Set r = Range("a1:g20")
r.Copy

Set outlookApp = CreateObject("Outlook.Application")
Set outMail = outlookApp.CreateItem(olMailItem)

With outMail
.To = Range("a22")
.Cc = Range("a23")
.Subject = Range("a24")
End With

outMail.Display
Set wordDoc = outMail.GetInspector.WordEditor
wordDoc.Range.PasteAndFormat wdChartPicture

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,013
Members
448,935
Latest member
ijat

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top