Apply Bold to Cell Value inside the text string of a HTMLbody - html

I am having trouble with applying Bold on a part of a sentence for an automatically generated email.
In my VBA script:
Public strBody As String
.HtmlBody = strBody
Dim FormulaCell As Range
Dim FormulaRange As Range
For Each FormulaCell In FormulaRange.Cells
With FormulaCell
I am able to Bold a whole sentence using "< b >" & "**Text Here**" & "< /b >" &
But it seems impossible to apply the same logic to a more complex sentence, or a referenced value.
strBody = "Hello, " & vbNewLine & vbNewLine & _
"Your task : " & Cells(FormulaCell.Row, "B").Value & " with the mention: " & Cells(FormulaCell.Row, "C").Value & " is nearing its Due Date: "
What I would like to Bold in the above sentence is the value of the FormulaCells.
strBody = "Hello, " & vbNewLine & vbNewLine & _
"Your task : " & **Cells(FormulaCell.Row, "B").Value** & " with the mention: " & **Cells(FormulaCell.Row, "C")**.Value & " is nearing its Due Date: "
So it would look something like that in my email:
Hello
Your task : Eat Potatoes with the mention: Potatoes are Delicious is nearing its Due Date:
Is this something that can be done?

edited
strBody = "<p>Hello</p>, " & vbNewLine & vbNewLine & _
"<p>Your task : <b>" & Cells(FormulaCell.Row, "B").Value & _
"</b> with the mention: <b>" & Cells(FormulaCell.Row, "C").Value & _
"</b> is nearing its Due Date: </p>"

Related

Create an email body in HTML that includes information from an object

I have a VBA object, aPosition.
I am trying to display information aPosition.something in an email.
My code gives me a syntax error.
strBody = "<HTML>" strBody & "<BR> & <p style='font-family:Trebuchet MS,sans-serif;font-size:22'>Equity position in sell: " & aPosition.SEC_NAME & " (" & aPosition.ISIN & ")" & " now in "& aPosition._RANKING" </p> & _
" Target Price: " & aPosition.TARGET_PRICE & "<BR>" & _
"<b><span style=""color:#C00000"">We have downgraded</span style=""color:#C00000""></b>" & aPosition.SEC_NAME & "<BR>" & " <U>Clients Impacted:</U><BR>"
You have a few problems when building your string.
First of all, you can use single quotes in your styles to eliminate the possible double-double quotes confusion.
Secondly, you seem to have strBody mixed in there twice for some reason.
Also, whatever aPosition is, you cannot have a property that starts with an underscore (aPosition._Ranking not valid), so I removed just that underscore. (so double check that part)
Try it like this:
strBody = "<HTML>" & strBody & "<BR><p style='font-family:Trebuchet MS,sans-serif;font-size:22'>Equity position in sell: " & aPosition.SEC_NAME & " (" & aPosition.ISIN & ") now in " & aPosition.RANKING & "</p>" & _
" Target Price: " & aPosition.TARGET_PRICE & "<BR>" & _
"<b><span style='color:#C00000'>We have downgraded</span style='color:#C00000'></b>" & aPosition.SEC_NAME & "<BR><U>Clients Impacted:</U><BR>"
The rule of thumb is to make sure you set a well-formed HTML markup to the HTMLBody property, for example, I see the following:
strBody = "<HTML>" strBody & "<BR>
If you want to modify the existing email body you need to paste your HTML pieces after the <body> tag and before the closing </body> tag.

Add spaces to a string to be used in .HTMLBody

I've tried several ways including making another string that acts as a space, and also "    ".
StrBody2 = "<b>Downtime particulars</b>" & "<br>" & _
"Short description: " & space(10) & _
Range("External_Start_Description").Text & "<br>" & "<br>"
I want a space between "Short description" and Range("External_Start_Description").Text.
I expect this has to do with using the range function in the string.
Context: this string is later used in an email with other strings:
.HTMLBody = "<p><font color=""#112369"" face=""arial"" size=""10px"">" & StrBody & StrBody2 & RangetoHTML(rng) & StrBody3
HTML collapses runs of multiple spaces to a single space - you need to use the non-breaking space entity if you want a run of spaces.
Replace(space(10), " ", " ")
All you need to do is put quotes with a space in your concatenation like below:
StrBody2 = "<b>Downtime particulars</b>" & "<br>" & _
"Short description: " & " " & Range("External_Start_Description").Text & "<br>" & "<br>"

How to design my html code inside my asp.net code?

How can i put some design in my HTML code inside the vb.net code?
I want to change the font design inside my body tag as the text in my body tag will send in users email address. Your help will greatly appreciated.
here's my code:
mail.Subject = "SYSTEM REQUEST FORM " & txtRequestTitle.Text & " " & Format(Now(), "MM/dd/yyyy").ToString
mail.IsBodyHtml = True
mail.Body = "<html>" & _
"<body>" & _
"Dear " & DropDownApprover.Text & ",<br><br>" & _
"" & "The following request has been created and need your approval" & "<br>" & _
"" & "===============================================" & "<br>" & _
"" & "http://10.190.193.162/ITrequestform/LoginPage.aspx" & "<br>" & _
"" & "===============================================" & "<br>" & _
"" & "Requested By: " & lblName.Text & "<br>" & _
"" & "Need to Approve By: " & DropDownApprover.Text & "<br>" & _
"" & "===============================================" & "<br>" & _
"" & "Request Title: " & txtRequestTitle.Text & "<br>" & _
"" & "Request Category: " & DropDownCateg.Text & "<br>" & _
"" & "Request Item: " & DropDownCateg2.Text & "<br>" & _
"" & "Date Needed: " & txtDateNeeded.Text & "<br>" & _
"" & "===============================================" & "<br>" & _
"" & "For Employee: " & txtRequestEmp.Text & "<br>" & _
"" & "Position: " & txtEmpPost.Text & "<br>" & _
"" & "Description of Request: " & txtDescription.Text & "<br>" & _
"" & "Justification of Request: " & txtJustification.Text & "<br>" & _
"" & "<br>" & _
"Thanks," & "<br>" & _
"HKT Teleservices""<br>" & _
"</body>" & _
"</html>"
Add CSS in project:
In SolutionExplorer --> Right click o project --> Add existing item and select Stylesheet(CSS).
Use CSS in ASPX page:
You need to add refererence for this CSS file in ASPX page
<link href="Stylesheet.css" rel="stylesheet" type="text/css" />
Any style you want to apply to your body text you can write in the code below
And in the Stylesheet.css file, , write this code
body{
font-family:"Helvetica Neue" , "Lucida Grande" , "Segoe UI";
font-size:20px;
}

how to use Unordered HTML List in excel vba code?

I have been trying to send a email via excel and use html in the message body but it seems excel does not reconize de <\li>. I have the microsoft html Oject library added.
Sub send()
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 = "dupma4"
'.CC = "Patrick.Blouin#mern.gouv.qc.ca;Veronic.Cinq-Mars#mern.gouv.qc.ca"
.BCC = ""
.Subject = "Ajouts d'adresse | [" & mun & " " & ville & "] | [" & Format(Now(), "yyyy-MM-dd") & "]"
.HTMLBody = "Bonjour," & "<br>" & "<br>" _
& "<ul>" & "<li> & message1 & "</li>" & "<li>" & message2 _
& "</li>" & "</ul>"
'.Send
End With
End Sub
You're missing an ending quotation mark (") for your first li. So it should be as follows...
.HTMLBody = "Bonjour," & "<br>" & "<br>" _
& "<ul>" & "<li>" & message1 & "</li>" & "<li>" & message2 _
& "</li>" & "</ul>"

Structuring HTML so text displays in one paragraph

I'm working on a VBA code in Excel to get input from reports and send emails.
I'm trying to avoid line breaks in my email body.
With olMail
.To = rep
.Subject = title & " - " & EndTitle
'.Recipients.Add rep
.Attachments.Add filePath & "\" & title & " - " & EndTitle & ".pdf"
.htmlBody = "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an email</u></b></BODY>" & "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an email</u></b></BODY>" _
& "<br>" & "<b>HK</b>" & "<b>" & amt & "</b>" _
& "<br>" & "Value date" & "<b>" & Trans & "</b>" & "<br>" &
initial
.Display
'.Send
End With
My email looked like this
Here is the email
Here is the email
What I want the email body to look like
Here is the email Here is the email
(on the same line)
Use this instead:
.htmlBody = "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an emailHere is an email</u></b>" _
& "<br>" & "<b>HK" & amt & "</b>" _
& "<br>" & "Value date" & "<b>" & Trans & "</b>" & "<br>" & initial & "</BODY>"
Or this, depending on which line breaks you want gone:
.htmlBody = "<BODY style=font-size:12pt;font-family:Arial><b><u>Here is an emailHere is an email</u></b>" _
& "<b>HK" & amt & "</b>" _
& "Value date" & "<b>" & Trans & "</b>" & initial & "</BODY>"
See how I encapsulated all the HTML between the opening and closing BODY tags?