VBA email format - html vs. plain

karolina1406

Board Regular
Joined
Apr 18, 2016
Messages
110
Office Version
  1. 365
Platform
  1. Windows
hi,
I am using vba to create an email from the UserForm. One of my TextBoxes (called "WorkInstructionsTX") in the form has multiline. How can I create an email so the information from this particular TextBox is multiline in an email body as well? I am using the below code:

.htmlbody = "Hi " & EngineerNameTX.Value & "," & "<br><br>" _
& "<b> text 1" & TOANoTX.Value & " </b> text 2" & SiteNameTX.Value & " on " & StartDateTX.Value & "< br > " _
& "</b>text 2" & "<br><br>" _
& "<b>Work required:" & "</b><br>" _
& WorkInstructionsTX.Text
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I'm assuming that line breaks in the text box are either character 10 or 13 (or both) - but in the html you need to use <br>

If so, could you use the replace function to change the line breaks? Before your line of code, put this:
Code:
HtmlWITX = WorkInstructionsTX.Text
HtmlWITX = Replace(HtmlWITX, chr(10), "<br>")
HtmlWITX = Replace(HtmlWITX, chr(13), "<br>")
HtmlWITX = Replace(HtmlWITX, "<br><br>", "<br>")
Then in your code, change WorkInstructionsTX.Text to HtmlWITX
This should convert the line break characters to html and prevent a double line break if both have been used.
 
Upvote 0
I'm assuming that line breaks in the text box are either character 10 or 13 (or both) - but in the html you need to use <br>

If so, could you use the replace function to change the line breaks? Before your line of code, put this:
Code:
HtmlWITX = WorkInstructionsTX.Text
HtmlWITX = Replace(HtmlWITX, chr(10), "<br>")
HtmlWITX = Replace(HtmlWITX, chr(13), "<br>")
HtmlWITX = Replace(HtmlWITX, "<br><br>", "<br>")
Then in your code, change WorkInstructionsTX.Text to HtmlWITX
This should convert the line break characters to html and prevent a double line break if both have been used.
hi,
thank you for this. but i think i am not sure where exacly in my code I need to put it. Can you re-write my code to include what you mentioned?
 
Upvote 0
They are separate lines before your code. Although your code covers a number of lines, it is a single piece of code setting the value of .htmlbody. So:
Code:
HtmlWITX = WorkInstructionsTX.Text
HtmlWITX = Replace(HtmlWITX, chr(10), "<br>")
HtmlWITX = Replace(HtmlWITX, chr(13), "<br>")
HtmlWITX = Replace(HtmlWITX, "<br><br>", "<br>")
.htmlbody = "Hi " & EngineerNameTX.Value & "," & "<br><br>" _
& "<b> text 1" & TOANoTX.Value & " </b> text 2" & SiteNameTX.Value & " on " & StartDateTX.Value & "< br > " _
& "</b>text 2" & "<br><br>" _
& "<b>Work required:" & "</b><br>" _
& HtmlWITX
 
Upvote 0
They are separate lines before your code. Although your code covers a number of lines, it is a single piece of code setting the value of .htmlbody. So:
Code:
HtmlWITX = WorkInstructionsTX.Text
HtmlWITX = Replace(HtmlWITX, chr(10), "<br>")
HtmlWITX = Replace(HtmlWITX, chr(13), "<br>")
HtmlWITX = Replace(HtmlWITX, "<br><br>", "<br>")
.htmlbody = "Hi " & EngineerNameTX.Value & "," & "<br><br>" _
& "<b> text 1" & TOANoTX.Value & " </b> text 2" & SiteNameTX.Value & " on " & StartDateTX.Value & "< br > " _
& "</b>text 2" & "<br><br>" _
& "<b>Work required:" & "</b><br>" _
& HtmlWITX
Fab! IT works perfectly! Thank you

p.s. i am self-taught in vba hence unless someone will show me what exactly I need to do - I struggle to put the puzzles together ;)
 
Upvote 0
No problem, I'm self-taught too - I suspect most of us are! These forums are great for picking up ideas and bits of code to help, but it's important to understand why they work - to make it easier to re-use/adapt.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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