Solved Outlook Message Format (1 Viewer)

Tommyf

New member
Local time
Yesterday, 22:22
Joined
Oct 4, 2021
Messages
4
Hi Everyone,

I am working with an older version of Acess and Outlook and have created a form that displays individual record information from a query. I added a Command Button for the user to use when sending an email to the person on the form. When Outlook opens, the message is on a single line. How could I add a new line to avoid one long line of text? Any guidance or direction is greatly appreciated!

Here is the code behind the Command Button:

Code:
Private Sub Command13728_Click()

    Dim Msg As String
        Msg = "Dear:" & " " & Me.FNAME.Value & "," & " " & _
         "Your expiration date is:" & "" & Me.EXPRDATE.Value
    
    Dim O As Outlook.Application
    Dim m As Outlook.MailItem
    
    Set O = New Outlook.Application
    Set m = O.CreateItem(olMailItem)
    
    With m
        
        .BodyFormat = olFormatHTML
        .HTMLBody = Msg
        .To = EMAIL
        .Subject = "Rideshare Renewal"
        .Display
        
        End With

        
        Set m = Nothing
        Set O = Nothing
        
        

End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:22
Joined
Oct 29, 2018
Messages
16,572
Hi. Welcome to AWF!

If you want to use HTML formatted message, then you would need to use HTML tags when building your message body.

To add a new line to an HTML message, you can use the <br> tag.
 

Tommyf

New member
Local time
Yesterday, 22:22
Joined
Oct 4, 2021
Messages
4
Thanks for the prompt response the DBguy! Please advise on how/where to add the <br> tag? I still have a lot to learn about VBA. Thank you.
Tommy
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:22
Joined
Mar 14, 2017
Messages
5,768
I think that if you're already using

.BodyFormat = olFormatHTML

Then you might be able to get away with simply throwing in some chr(10) & chr(13) 's to your existing Body.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:22
Joined
Oct 29, 2018
Messages
16,572
Thanks for the prompt response the DBguy! Please advise on how/where to add the <br> tag? I still have a lot to learn about VBA. Thank you.
Tommy
Well, let's say the message you want to say is this:
Code:
Dear Mr. Customer,

Please review the following information...
If you create your message in code using the following:
Code:
strMsg = "Dear Mr. Customer," & vbCrLf & vbCrLf & "Please review the..."
You might actually get something like this in an HTML email.
Code:
Dear Mr. Customer,Please review the following...
So, to avoid that, try replacing all vbCrLf with a <br> tag. For example:
Code:
strMsg = "Dear Mr. Customer,<br><br>Please review the following..."
Hope that helps...
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:22
Joined
Mar 14, 2017
Messages
5,768
This works fine for me - displays line breaks appropriately w/out HTML.

Code:
Sub test()
Dim outlookapp As New Outlook.Application
Dim mi As Outlook.MailItem
Set mi = outlookapp.CreateItem(olMailItem)
mi.BodyFormat = olFormatHTML

mi.Body = "Line1" & Chr(10) & Chr(13) & "Line2" & Chr(10) & Chr(13) & "Line3"

mi.Save
mi.Display

End Sub
 

Tommyf

New member
Local time
Yesterday, 22:22
Joined
Oct 4, 2021
Messages
4
The responses
I think that if you're already using

.BodyFormat = olFormatHTML

Then you might be able to get away with simply throwing in some chr(10) & chr(13) 's to your existing Body.
Thank you!

Tommy
 

Tommyf

New member
Local time
Yesterday, 22:22
Joined
Oct 4, 2021
Messages
4
This works fine for me - displays line breaks appropriately w/out HTML.

Code:
Sub test()
Dim outlookapp As New Outlook.Application
Dim mi As Outlook.MailItem
Set mi = outlookapp.CreateItem(olMailItem)
mi.BodyFormat = olFormatHTML

mi.Body = "Line1" & Chr(10) & Chr(13) & "Line2" & Chr(10) & Chr(13) & "Line3"

mi.Save
mi.Display

End Sub
That worked! Thank you the DBguy!
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:22
Joined
Oct 29, 2018
Messages
16,572
This works fine for me - displays line breaks appropriately w/out HTML.

Code:
Sub test()
Dim outlookapp As New Outlook.Application
Dim mi As Outlook.MailItem
Set mi = outlookapp.CreateItem(olMailItem)
mi.BodyFormat = olFormatHTML

mi.Body = "Line1" & Chr(10) & Chr(13) & "Line2" & Chr(10) & Chr(13) & "Line3"

mi.Save
mi.Display

End Sub
Hi Isaac,

Try using mi.HTMLBody instead of mi.Body.
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:22
Joined
Mar 14, 2017
Messages
5,768
Hi Isaac,

Try using mi.HTMLBody instead of mi.Body.

The code that I posted works for the line breaks. You can either use Body (as I did), or HTMLBody (and then have to use HTML).

Which is easier?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:22
Joined
Oct 29, 2018
Messages
16,572
The code that I posted works for the line breaks. You can either use Body (as I did), or HTMLBody (and then have to use HTML).

Which is easier?
It's not about which is easier. The OP's first post used

BodyFormat = olFormatHTML

which implied a need for an HTML formatted email.

Line breaks don't make a difference in either format. If the OP doesn't need HTML formatted email, then your solution is just as good.

However, if there's any requirement to include bold or italicized or colored fonts or a table of data in the email, then the email must be formatted in HTML, and line breaks have to be added using <br>.

Hope that makes sense...
 

Isaac

Lifelong Learner
Local time
Yesterday, 22:22
Joined
Mar 14, 2017
Messages
5,768
It's not about which is easier. The OP's first post used

BodyFormat = olFormatHTML

which implied a need for an HTML formatted email.

Line breaks don't make a difference in either format. If the OP doesn't need HTML formatted email, then your solution is just as good.

However, if there's any requirement to include bold or italicized or colored fonts or a table of data in the email, then the email must be formatted in HTML, and line breaks have to be added using <br>.

Hope that makes sense...

Of course, it's just his original post didn't actually include any HTML related stuff in the body, so it seems unnecessary to go that route..
Msg = "Dear:" & " " & Me.FNAME.Value & "," & " " & _
"Your expiration date is:" & "" & Me.EXPRDATE.Value
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:22
Joined
Oct 29, 2018
Messages
16,572
Of course, it's just his original post didn't actually include any HTML related stuff in the body, so it seems unnecessary to go that route..
I see what you mean. I took it this way. If the intent is to compose an HTML formatted email, one would use the .HTMLBody property. If the intent is to simply send a plain text email, then that would normally mean just using the .Body property. In the OP's sample code, both olFormatHTML and .HTMLBody were used, so I took it that the intent was to send an HTML formatted email, even though the posted sample email message had no HTML tags in it.

In any case, I just wanted to make sure future readers understand that difference. Either way works, just depends on the user's intent. That's all.
 

Users who are viewing this thread

Top Bottom