New Line for VBA E-Mail

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
296
Office Version
  1. 2016
Platform
  1. Windows
All I am using this code:

VBA Code:
Sub SendEmail()
    Dim appOutlook As Outlook.Application
    Dim mEmail As Outlook.MailItem

    Set appOutlook = New Outlook.Application
    Set mEmail = appOutlook.CreateItem(olMailItem)

    With mEmail
         .To = "testmymail@outlook.com"
         .CC = ""
         .BCC = ""
         .Subject = "MHT Equipment Maintenance Audit"
         .HTMLBody = "All," & vbNewLine & _
                      "Attached is the Monthly MHT Level II Preventative Maintenance Audit spreadsheet."
         .Display
    End With
    Set mEmail = Nothing
    Set appOutlook = Nothing
End Sub


But when I run it it looks like this.... All,Attached is the monthly.......... I need a space after the "All,"

Any help would be appreciated. thank you
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi RandyD123
VBA Code:
.HTMLBody = "All, " & vbNewLine & _
                      "Attached is the Monthly MHT Level II Preventative Maintenance Audit spreadsheet."
 
Upvote 0
Hi RandyD123
VBA Code:
.HTMLBody = "All, " & vbNewLine & _
                      "Attached is the Monthly MHT Level II Preventative Maintenance Audit spreadsheet."
I'm pretty sure I tried that but I didn't get a space. I should be clearer, I think what I need is a line break so All, is on the top line then a new line for the rest. And maybe a 3rd line. Thanks.
 
Upvote 0
Also, unless the email needs to be in HTML, the vbNewLine's should work if you change the property from ".HTMLBody" to just ".Body"
 
Upvote 0
Also, unless the email needs to be in HTML, the vbNewLine's should work if you change the property from ".HTMLBody" to just ".Body"
I believe this will work. I forgot that I'm not technically using VB code in the body of the e-mail. I will try both ways tomorrow and let you know. Thank You!!!
 
Upvote 0
Well I guess that didn't work after all. I error out on the second line:
VBA Code:
Sub SendEmail()
    Dim appOutlook As Outlook.Application
    Dim mEmail As Outlook.MailItem

    Set appOutlook = New Outlook.Application
    Set mEmail = appOutlook.CreateItem(olMailItem)

    With mEmail
         .To = "someone@someplace.com"
         .CC = ""
         .BCC = ""
         .Subject = "MHT Equipment Maintenance Audit"
         .HTMLBody = "All,<br>"
                      "Attached is the Monthly MHT Level II Preventative Maintenance Audit spreadsheet."
         .Display
    End With
    Set mEmail = Nothing
    Set appOutlook = Nothing
End Sub

The "Attached......" line is in red.
 
Upvote 0
I got this to work. But I can't have .HTMLBody I can only use .Body.

VBA Code:
Sub SendEmail()
    Dim appOutlook As Outlook.Application
    Dim mEmail As Outlook.MailItem

    Set appOutlook = New Outlook.Application
    Set mEmail = appOutlook.CreateItem(olMailItem)

    With mEmail
         .To = "someone@someplace.com"
         .CC = ""
         .BCC = ""
         .Subject = "MHT Equipment Maintenance Audit"
         .Body = "All, " & vbNewLine & _
         "" & vbLf & _
                   "Attached is the Monthly MHT Level II Preventative Maintenance Audit spreadsheet." & vbNewLine & _
         "" & vbLf & _
                   "Some sort of text will go here."
         .Display
    End With
    Set mEmail = Nothing
    Set appOutlook = Nothing
End Sub
 
Upvote 0
The HTML version of the code doesn't work because you need a line continuation character for the string assigned to .HTMLBody.

This should work:

VBA Code:
Option Explicit

Sub SendEmail()
    Dim appOutlook As Outlook.Application
    Dim mEmail As Outlook.MailItem

    Set appOutlook = New Outlook.Application
    Set mEmail = appOutlook.CreateItem(olMailItem)

    With mEmail
         .To = "someone@someplace.com"
         .CC = ""
         .BCC = ""
         .Subject = "MHT Equipment Maintenance Audit"
         .HTMLBody = "All,<br>" & _
         "Attached is the Monthly MHT Level II Preventative Maintenance Audit spreadsheet."
         .Display
    End With
    Set mEmail = Nothing
    Set appOutlook = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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