VBA - Automatically Break the Line in HTML format.

Attrazion

New Member
Joined
Mar 11, 2023
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi! I have on code below which fetch data from my excel file ( I cannot show it since its a confidential ) I just want to put a automatically break on each line, I already did it using <br> but when I put multiple data on one cell, it stays on single line. The sample is on Data1. Can anyone help me?

VBA Code:
Sub SendEmail()



Dim OutApp As Object, OutMail As Object


TodayDate = Format(Date, "mm/dd/yyyy")

Dim Data1 As String
Dim Data2 As String
Dim Data3 As String


' (For example this is the data that I pulled from Excel File) (When I triggered the Run button, It stays on one line like Name1Name2Name3

Data1=   Name1, Name2, Name3 
Data2= Name4
Data3= Name5

I want the output like:  
Name1 1st line
Name2 2nd line
Name3 3rd line
Name4 4th line
Name5 4th line





'Dim OutApp As New Outlook.Application
'Dim OutMail As Outlook.MailItem

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


'HTML Contents including CSS
strHtml1 = "<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='[URL='http://schemas.microsoft.com/office/2004/12/omml%27']http://schemas.microsoft.com/office/2004/12/omml'[/URL] xmlns=' strHtml2 = "{margin:0in; font-size:11.0pt; font-family:'Calibri',sans-serif;} a:link, span.MsoHyperlink {mso-style-priority:99; color:#0563C1; text-decoration:underline;} span.EmailStyle17 {mso-style-type:personal-compose; font-family:'Calibri',sans-serif; color:windowtext;} .MsoChpDefault {mso-style-type:export-only; font-family:'Calibri',sans-serif;} @page WordSection1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in;} div.WordSection1 {page:WordSection1;} --></style><!--[if gte mso 9]><xml> <o:shapedefaults v:ext='edit' spidmax='1026' /> </xml><![endif]--><!--[if gte mso 9]><xml> <o:shapelayout v:ext='edit'> <o:idmap v:ext='edit' data='1' /> </o:shapelayout></xml><![endif]--></head> "


strBody1 = "<body lang=EN-US link='#0563C1' vlink='#954F72' style='word-wrap:break-word'>"
strBodyDiv = "<div class=WordSection1><p >Hi All,<br><br>Please see the table below for the Test Table.<o:p></o:p></p> <p style='margin-bottom:12.0pt'><span style='color:black;background:white'><a href='testlink[URL='http://sn-sos:8080/secure/Dashboard.jspa?selectPageId=19909%27']'[/URL] target='_blank'><span style='font-size:10.0pt;font-family:'Segoe UI',sans-serif'>testlink[URL='http://sn-sos:8080/secure/Dashboard.jspa?selectPageId=19909']</span>[/URL] </a> </span> <o:p></o:p> </p> "


strBodyTable = "<table border=0 cellspacing=0 cellpadding=0 style='border-collapse:collapse'><tr style='height:17.95pt'><td width=882 valign=top style='width:661.25pt;border:solid windowtext 1.0pt;background:#002060;padding:0in 5.4pt 0in 5.4pt;height:14.95pt'><p ><b><span style='font-size:10.0pt;font-family:'Times New Roman',serif;color:white'>Manila Pictures</span></b><o:p></o:p></p></td></tr><tr"

strBodyTable2 = "style='height:17.95pt'><td width=882 valign=top style='width:661.25pt;border:solid windowtext 1.0pt;border-top:none;background:white;padding:0in 5.4pt 0in 5.4pt;height:17.95pt'><p ><span style='color:black'></span><o:p></o:p></p></td></tr><tr style='height:17.95pt'><td width=882 valign=top style='width:661.25pt;border:solid windowtext 1.0pt;border-top:none;background:#1F3864;padding:0in 5.4pt 0in 5.4pt;height:14.95pt'>"


strBodyTable3 = "<p ><b><span style='font-size:10.0pt;color:white'>Names</span></b><o:p></o:p></p></td></tr><tr style='height:5.9pt'><td width=882 valign=top style='width:300.25pt;border:solid windowtext 1.0pt;border-top:none;padding:0in 5.4pt 0in 3.0pt;height:10.0pt'>"

strBodyTableQA = Data1 & "<br>" & Data2 & "<br>" & Data3 & "<br>"  'Here is the data

strBodyTable4 = "</td></tr>"

strEndHtml = strHtml1 & strHtml2 & strBody1 & strBodyDiv & strBodyTable & strBodyTable2 & strBodyTable3 & strBodyTableQA & strBodyTable4 & "</table>" & "</div>" & "</body>" & "</html>"

ToEmail = "sampleemail123@email.com"

With OutMail
.To = ToEmail
.Subject = "Sample Subject" & " " & ScrumDate
.CC = "sampleemail@email.com" & ";" & "sampleemail1@email.com" & ";" & "sampleemail2@email.com"
.Display

.HTMLBody = strEndHtml & .HTMLBody

End With

Set OutApp = Nothing
Set OutMail = Nothing

End Sub

So the Output should be
Name1
Name2
Name3
Name4
Name5
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
When you fill Data1 just add "<br>" between the names.
If this is not sufficient then specify from where "Name1, Name2, Name3" are fetched
 
Upvote 0
When you fill Data1 just add "<br>" between the names.
If this is not sufficient then specify from where "Name1, Name2, Name3" are fetched
Hi, Name1 - Name3 are fetched from Excel Value. I cannot show the true excel since it's confidential that's why I put dummy data.

For example:
The Data1 fetch names from excel so if the excel cells has 2 or more fetched data that's my problem starts.

The Data1 fetched 3 names from excel, so the value will be Name1 Name2 Name3 in single line, I want instead of single line, I want it to view it or as multiple like below.
Name1
Name2
Name3


So what ever I put or fetched on Data 1 from Excel Value it should be new line.

Sorry for bad English, I hope you understand it well.
 
Upvote 0
Try
VBA Code:
.HTMLBody = Replace(strEndHtml, Chr(10), "<br>", , , vbTextCompare) & .HTMLBody


If that doesn't work, and you cannot leak confidential information about the Manila Pictures, like how the names are organized on the worksheet and which vba instructions you use to fetch them, then try posting on www.thesorcerer.com
 
Upvote 0
Try
VBA Code:
.HTMLBody = Replace(strEndHtml, Chr(10), "<br>", , , vbTextCompare) & .HTMLBody


If that doesn't work, and you cannot leak confidential information about the Manila Pictures, like how the names are organized on the worksheet and which vba instructions you use to fetch them, then try posting on www.thesorcerer.com
Hi this one is working, but the Name3 is combining the with the last data from Name2

Example the output is:
Name1
Name2Name3
 
Upvote 0
strBodyTableQA = Data1 & "<br>" & Data2 & "<br>" & Data3 & "<br>" 'Here is the data
This one is working now, thank you very much!!! My second problem is to sort the name using the first characters in the cell value.
 
Upvote 0
Thank you for the feedback
If you have new problems than it would be better to open a new discussion
 
Upvote 0
Thank you for the feedback
If you have new problems than it would be better to open a new discussion
Hi, thanks once again. I already post a thread regarding my new problem. Thanks!

 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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