Structuring HTML so text displays in one paragraph - html

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?

Related

How to modify bullets?

How do I format the bullet point itself?
The bullet points all appear as stars instead of a normal black circle for the first bullet and a hollow circle for the sub-bullet.
I tried to define the style but my code gives me an error message when I add list-style-type:disc;.
Using w3schools this is how I understand it should work.
Sub Email_Budget()
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)
Dim CaseCount As Long
CaseCount = WorksheetFunction.CountA(Range("B6:B500"))
Debug.Print CaseCount
Dim i As Integer
With objEmail
.To = "abc#xyz.com"
.Subject = "TEST1: May 2019 Budget"
.Body = "Kurt,"
.Body = .Body & " "
.Body = .Body & "The potential " & MonthName(Month(ActiveSheet.Range("A2"))) & " invoices are below."
.Body = .Body & " "
For i = 1 To CaseCount
If ActiveSheet.Cells(i + 5, 4).Value = "Yes" Then
.HTMLBody = .HTMLBody & "<ul style='list-style-type:disc;'>" & "<li>" & ActiveSheet.Cells(i + 5, 2).Value & " - " & Format(ActiveSheet.Cells(i + 5, 6).Value, "Currency") & " (" & Format(ActiveSheet.Cells(i + 5, 8).Value, "Currency") & " without budget or invoicing)." & "</li>" & "<ul>" & "<li>" & "Last billed " & ActiveSheet.Cells(i + 5, 10) & "." & "</li>" & "</ul>" & "</ul>"
End If
Next i
.Body = .Body & " "
.Body = .Body & " "
.Body = .Body & "Thank you,"
.Body = .Body & "Karen"
.Display
End With
End Sub
In order to create multi-level list, you can simply construct another <ul> nested beneath one of your <li> items. You can tinker with HTML here:
http://w3schools.com/code/tryit.asp?filename=G3UBZA6VM2WS
To control the list item style for an individual <li> item, specify it's type attribute`.
To set the list item style for all items in a list, specify the style attribute with the appropriate css, e.g.:
"<ul style='list-style-type:disc'>"
Note the use of single-quotes within the quoted string :)
Finally, and I now suspect that this was your problem all along (disc style appears to be the default in Outlook, so you probably don't need to specify it as such):
You can't mix .Body and .HTMLBody. You need to use one or the other. If you use .Body, the email will be plain text and will not accept or render HTML. So you should convert all of your .Body assignments to the appropriate .HTMLBody instead. Untested, but should be something like this:
With objEmail
.To = "abc#xyz.com"
.Subject = "TEST1: May 2019 Budget"
.HTMLBody = "Kurt,"
.HTMLBody = .HTMLBody & "<br>"
.HTMLBody = .HTMLBody & "<p>The potential " & MonthName(Month(ActiveSheet.Range("A2"))) & " invoices are below.</p>"
.HTMLBody = .HTMLBody & "<br>"
For i = 1 To CaseCount
If ActiveSheet.Cells(i + 5, 4).Value = "Yes" Then
.HTMLBody = .HTMLBody & "<ul style='list-style-type:disc;'>" & "<li>" & ActiveSheet.Cells(i + 5, 2).Value & " - " & Format(ActiveSheet.Cells(i + 5, 6).Value, "Currency") & " (" & Format(ActiveSheet.Cells(i + 5, 8).Value, "Currency") & " without budget or invoicing)." & "</li>" & "<ul>" & "<li>" & "Last billed " & ActiveSheet.Cells(i + 5, 10) & "." & "</li>" & "</ul>" & "</ul>"
End If
Next i
.HTMLBody = .HTMLBody & "<br><br>"
.HTMLBody = .HTMLBody & "<p>" & "Thank you," & "</p>"
.HTMLBody = .HTMLBody & "<p>" & "Karen" & "</p>"
.Display
End With

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>"

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

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>"

Problems in HTA

So I'm trying to use ScriptGUI (from here) for my .bat files, but it doesn't have a file selector, so I attempted to add one.
I just copied and renamed some code around and it seems to work, apart from adding another variable for the subfunction thing.
' build a file selector
ElseIf UCase(strSplit(0)) = "FILE" Then
strHTML = strHTML & Build_File(strLabel,id,Replace(strLabel," ",""),strSplit(2))
' store the batch file in the arrControls array
arrControls(id) = "file,none"
id = id + 1
>
the problem seems to be the code under here "Click_File(" & fiId & "," & fiFilter & ")"
Function Build_File(fiLabel, fiId, fiName, fiFilter)
' Construct a file selector
Dim strHTML
strHTML = "<input class='button' type='button' name='" _
& fiName & "' value='" & fiLabel & "' id='" & fiId _
& "' onClick=" & chr(34) & "Click_File(" & fiId & "," & fiFilter & ")" & chr(34) _
& " onMouseOver=" & chr(34) & fiName & ".className='button btnhov'" & chr(34) _
& " onMouseOut=" & chr(34) & fiName & ".className='button'" & chr(34) _
& ">"
strHTML = strHTML & " <input type='text' readonly='readonly' value='none' name='fi" & fiName & "' id='fi" & fiId & "'/> "
Build_File = strHTML
End Function
>
Sub Click_File(strId, fiFilter)
' open a file selector
set objShell= CreateObject("WSCript.Shell")
myCur = objShell.CurrentDirectory
Dim file
file = GetFileName(myCur, fiFilter)
arrControls(strId) = "file," & file
document.getElementById("fi" & strId).value = file
document.getElementById("fi" & strId).size = Len(file) + 2
End Sub
It tells me I can't use parenthesis when calling a sub.
Any ideas?
EDIT: new problem,
I'm using a script from Rob van der Woude for the open file dialog (top post from here) which is apparently supposed to work in hta but I get an error saying "ActiveX component can't create object: 'UserAccounts.CommonDialog'"
Well, the message isn't wrong. You can't use parentheses when calling a Sub.
Change this line from:
& "' onClick=" & chr(34) & "Click_File(" & fiId & "," & fiFilter & ")" & chr(34) _
to:
& "' onClick=" & chr(34) & "Click_File " & fiId & "," & fiFilter & chr(34) _
and see if that solves your problem.