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.
Related
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 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".
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
‑
I want to create a template with XML or HTML with placeholders for exception reporting variables to be swapped in at runtime when an exception occurs. I like what I've got so far with the template, and behavior, but it's all hard coded at the moment, so bad.
Here's a sample of the code I have...
'Styling (Outlook does not follow CSS standards >:-( )
body = "<html><head>" & _
"<style type=""text/css"">" & _
" * {font:12px Arial, Helvetica, sans-serif;}" & _
" table {border-collapse:collapse;}" & _
" table.grid td {padding:0 7px;border:solid #ccc 1px;}" & _
" .bold {font-weight:bold;padding-right:12px;}" & _
"</style></head><body>"
'Partial template
body &= "<table><tr><td class=""bold"">Error:</td><td>" & ex.Message & "</td></tr>" & _
"<tr><td class=""bold"">Exception:</td><td>" & ex.GetType().FullName & "</td></tr>" & _
"<tr><td class=""bold"">Source:</td><td>" & ex.Source & "</td></tr>" & _
"<tr><td class=""bold"">Request Url:</td><td>" & Request.Url.ToString & "</td></tr></table><br />"
Notice the ex.Message variable, etc. I want to move all the HTML code into a separate file (XML or HTML, depending on what is recommended, XML I imagine?) along with placeholders for each of the error variables. Then load the template, substitute in the variables and send the email on the fly. What is the best practice for doing this?
Also, please don't try to fix my CSS, that's not what this question is about. Outlook doesn't follow standards (go figure), and this is how I had to do it.
Thanks. ;)
I think you are pretty much on the right way. The steps would be:
Extract the templates into a resource file (the resource extension .xml, .html is the actual resource type).
Replace the variable concatenations with tokens; So would be:
(...)<tr><td class="bold">Source:</td><td>{exSource}</td></tr>(...)
Create an extension method to String objects to "enhance" the String.Format, so you could do something like
' Load the resourceFile and put content into formattedBody
Dim formattedBody as String = templateBody.FormatWith(New With{.exSource = ex.Source})
About how to implement the FormatWith, take a look here.
This answer may help you as well.