I have the following code written in VBA:
Dim StrBody As String
StrBody = "Text Line 1" & "<br>" & _
"Text Line 2" & _
How do I change the font to get a particular style (e.g. Arial), size, and color?
The text is used as the body of an e-mail. That's why some better looking style needs to be used. The default font is "Times New Roman".
I imagine you would have to use the font tag and its various attributes:
StrBody = "<font size=""10"" face=""Arial"" color=""red"">" & _
"Text Line 1" & "<br>" & _
"Text Line 2" & _
"</font>"
Make sure the " quotes used to surround HTML attribute values don't get interpreted as the end of the VBA string: escape them by writing "" instead, as in the example above.
color can also be specified as hexadecimal RGB codes. So color="red" could be replaced by the more general color="#FF0000".
Related
I am trying to change the font size of certain parts of my email to size 12pt.
11pt and 13pt both work. When trying to set 12pt font it comes out at size 10pt.
Here is a portion of my code:
.HTMLBody = "Hello All,<br><br>" & _
"<B><U><font style=""font-size:13pt"">Incidents:</B></U></font><br><br>" & _
ES11Text & _
"<B><U>Non Intervention Incidents:</B></U><br><br>" & _
NonInterventionText & _
"<B><U>Hand-off:</B></U><br><br>" & _
"N/A<br><br>" & _
"<B><U>Escaltions:</B></U><br><br>" & _
Escalations & _
"Regards,<br>" & _
Signature
I figured it out. In my code to generate an email I wanted to include my signature at the bottom so I set a string equal to OutMail.HTMLBody before doing anything else and then concatenate that string to the end of my email which will add my signature. Doing this is what srews it up. If I don't include the signature, Times New Roman 12pt font works... Still doesn't make sense to me.
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.
I have an Excel macro that creates and sends emails. I want to set the background colour to match an inserted image. It is a dark blue shade so I will also look at changing the text to white.
Most searches show results for table background not the entire email body background. Is it possible to change the background of an Outlook email in VBA using HTML Body?
xHTMLBody = "<span LANG=EN>" _
& "<Body style = bgcolor=”#1b1c37”>" _
& "<p class=style2><span LANG=EN><font FACE=Calibri SIZE=3>"
_
& "<p>Dear " + Worksheets("SHeet2").Range("T3") + ",</p></p>
</p>" _
& "<p>The weekly results .</p></p>" _
& "<br>" _
& "<IMG align=baseline border=0 hspace=0 src=cid:myident>" +
",</p></p></p>" _
& "<br>If you have any questions feel free to give me a call
</font></Body></span>"
The code produces an email but "BGcolor" isn't changing anything.
Try the following instead:
"<Body style="bgcolor=#1b1c37">"
or
"<Body style="backgroundcolor=#1b1c37">"
Make sure that you create a well-formed HTML markup. I'd recommend testing your sample HTML markup in Word because Outlook uses it for rendering message bodies.
I am running into a situation in which I am using VBA to generate HTML code that will go into an Outlook email. The HTML code has a bunch of images in it and I am storing the image path names in string variables. For Outlook to recognize the image, it needs quotes around it in the format:
<img src="filepath">
The problem is when I write the VBA code to contain the quotation marks, it doesn't realize the variable is a variable and instead inputs it as text. Here is the code I am using (this is just the snippet causing problems:
StageID = "C:\Users\xxxxx\Desktop\stage1.png"
Body = Body & "<TR><TD width=""100"" rowspan=""6"" align=""middle""><img src="""" & StageID & """" align=""middle"" width=""100"" height=""200""></td></tr>
You can use single quotes for HTML attribute values:
StageID = "C:\Users\xxxxx\Desktop\stage1.png"
Body = Body & "<TR><TD width='100' rowspan='6' align='middle'>" & _
"<img src='" & StageID & "' align='middle' width='100' " & _
" height='200'></td></tr>"
You have one too many inverted commas. try this:
body = body & "<TR><TD width=""100"" rowspan=""6"" align=""middle""><img src=""" & StageID & """ align=""middle"" width=""100"" height=""200""></td></tr>"
3 inverted commas either side of the & for the variable substitution works for me.
I was passing a string of HTML formatted text for a signature in VB6 to Outlook but on certain very old computers with very old versions of outlook it was running into a problem where hypens would start a new line, or if I used the tag to fix it then word wrap would be totally turned off.
I want to try to use RTF instead hoping that this will fix the problem. However I still have that signature that is in HTML and I need to pass it in as a string into Outlook using Rich Text formatting. I can't find any good resources on this or if it is even possible, whenever I try they show up with the escape commands and everything.
My current HTML string:
string = vbCrLf & vbCrLf & "<B><FONT face=Arial color=#365f91 size=2>" & _
strName & "</FONT>" & _
"<FONT face=Arial size=2><BR>" & _
"<I>" & strPosition & "</I>" & _
"</FONT></B><BR/><B><FONT face=Arial size=2>" & _
strAddress1 & "</FONT></B><BR/><FONT face=Arial size=1>" & _
strAddress2 & "<BR>" & strCity & ", " & _
strProvince & ", " & strPostalCode & _
"<BR>" & strCountry & "<BR>Office: " & strPhone & _
"<BR>" & strEmail & "<BR>www.website.com<BR>" & _
" " & _
"<I><FONT face=Arial size=2>" & strImageCaption & "</FONT>" & _
"<BR/><BR/><BR/></I><FONT face=Arial size=1>" & strDisclaimer & _
"</FONT></P>"
Anyone able to help me pass this in as rtf or know of anyway I could do this? Will be very appreciated!
EDIT: Alternatively if anyone knows how to fix the issue with word-wrap/line-breaking hyphens I would love that too.
This can be the RTF template idea, produced by wordpad and slightly altered. I assume you can mixin the VB stuff to replace the variable text.
To get this:
Hello
postion
My‑street
Mystreet 2
My city
My disclaimer
( I don't how to do color in SO markup but in the RTF the first line in RED and My disclaimer is one point smaller)
use this RTF:
{\rtf1\ansi\ansicpg1252\deff0\deflang1043{\fonttbl{\f0\fnil\fcharset0 Arial;}}
{\colortbl ;\red255\green0\blue0;}
\cf0\b\fs22 Hello\par
\cf0\b0\i\fs24 postion\par
\i0 My\_street\par
Mystreet 2\par
My city\par
\fs22 My disclaimer\par
\f1\par
}
between My and street I placed a non breaking hyphen, according to the RTF specs. In html the same could be achieved by using
‑