XML / HTML Email Template for Error Reporting - html

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.

Related

Setting email body background color in a VBA created HTML Outlook email

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.

"Expected End of Statement" when concatenating string

I am attempting to embed an PDF iframe viewer into a web based form I am building.
I have done this multiple times but for the life of me I can not get it right this time.
''This attaches a PDF uploaded on a previous form and should display it within
''an iFrame.
If "aObjects("RD20_AttachRandR")" <> "avar1" Then
fcLabel = "<iframe src=""" & "aObjects("RD20_AttachRandR ")" & ".PDF" & _
" width=800px height=1000px ><p>Your browser does not support iframes.</p></iframe>"
End If
Somewhere in the line starting with fcLabel I am missing an " that ends the string that I am passing through. But I am unable to find it.
Presumably aObjects is a dictionary (or other collection) variable, so you need to remove the outer double quotes. Also, the second time you use that variable the item name string has a trailing space ("RD20_AttachRandR ") which you may want to remove.
If aObjects("RD20_AttachRandR") <> "avar1" Then
fcLabel = "<iframe src=""" & aObjects("RD20_AttachRandR") & ".PDF" & _
" width=800px height=1000px ><p>Your browser does not support iframes.</p></iframe>"
End If
As noted by Ansgar Wiechers,
I had forgotten to remove a trailing space at the end of the aObject name.
I removed it and it works now.
Ta.

Quotations in HTML Code generated by VBA

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.

how to load filename, filesize, reciveddate using SSIS, to a Database

I am very new to the world of ETL, though I have done couple of ETL using SSIS since I started learning ETL like 3 months ago. I have a good question for anybody with the experience.
. I want to populate a table in a database I created with files information ( Filename, FileSize and ReceivedDate ) from a folder. Using SSIS package. An example is loading file information in the path below in a sql database.
C:\Users\Documents\newfiles\purchaseorder. C:\Users\Documents\newfiles\invoice.
Also note I have multiple types of file. csv,edi,tcf etc.
Can anyone kindly give a step by step guide to solving this problem?
Thanks.
The simplest way to get that sort of information is to use the .NET Framework's System.IO.FileInfo class in a Script Task:
public void Main()
{
var receivedFile = (string)Dts.Variables["User::ReceivedFile"].Value;
var fileInfo = new System.IO.FileInfo(receivedFile);
Dts.Variables["User::FileName"].Value = fileInfo.FullName;
Dts.Variables["User::FileSize"].Value = fileInfo.Length;
Dts.Variables["User::ReceivedDate"].Value = fileInfo.CreationTime;
// or whatever other information you may need
Dts.TaskResult = (int)ScriptResults.Success;
}
The code above assumes that you've set the User::ReceivedFile variable to the full path of whatever file you're dealing with; this is typically (but not always) done through a File System Task. Obviously, when configuring the Script Task, you'll need to specify ReadWrite access for the variables you'll be writing to.
Once you have that information in variables, of course, you can use it in whatever manner is needed.
For further information on the FileInfo class, see MSDN here.
this is piece of code which I am using to extract the info...
HHInfo = New FileInfo(HHFile(i).ToString())
HHFileDate = HHInfo.CreationTime
HHDate = HHFileDate.ToString("dd/MM/yyyy")
writer.Write("HHInfo.CreationTime : " & HHInfo.CreationTime.ToString & vbNewLine & vbNewLine)
writer.Write("HHInfo.LastAccessTime : " & HHInfo.LastAccessTime.ToString & vbNewLine & vbNewLine)
writer.Write("HHInfo.LastWriteTime : " & HHInfo.LastWriteTime.ToString & vbNewLine & vbNewLine)
HHElapsedTime = New DateTime(((HHInfo.LastWriteTime - HHInfo.CreationTime).Ticks))
EndTimelist.Add(HHInfo.LastWriteTime)
writer.Write("HHElapsedTime : " & HHElapsedTime.ToString("HH:mm:ss") & vbNewLine & vbNewLine)
swriter.WriteLine("HH Start Time : " & HHInfo.CreationTime.ToString & vbNewLine)
swriter.WriteLine("HH End Time : " & HHInfo.LastWriteTime.ToString & vbNewLine)

Signature needs to be in RTF instead of HTML. Passing in from VB6 to Outlook

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
‑