Use VBA to add <style> tag to Email.

Status
Not open for further replies.

JimG

New Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Hi. I've been experimenting with VBA for Outlook 2013. When I use my HTML in an Outlook HTML email message, it strips most of my styles from the <style> tag. I have my styles inline also, but when Outlook strips my <style> tag, it also removes my @media queries. A large percentage of my end-users will read my email using an iPhone, so I'd like to take advantage of @media. I'm forced to use Outlook 2013 to send the message by my employer.

With the VBA script below, I'm trying to replace the default <style> tag in Outlook HTML email with my own. Does anyone know if this can be done, or what may be wrong with my code below?

Public Sub routine()
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = myRegExp.Replace("[<style.*</style>]", "[>table.MsoNormalTable {font-size:1.5em;font-family:Arial,sans-serif;}div.MsoNormal {margin:0in;margin-bottom:.0001pt;font-size:1.5em;font-family:Arial,sans-serif;}.jim {font-size:5em;}@media only screen and (max-width:580){table.MsoNormalTable{font-size:3em;font-family:Arial,sans-serif;}}</style>")
End Sub


Thanks for any advice,

Jim
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
My knowlegde about regex is 0. However, what I see is that your code has not relation to any email. Probably, one of the Replace arguments must point to the HTMLBody property of the email you want to edit.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
i know a little more about regex than Michael, but I've not used replace in a pattern. MSDN uses this is an example:

Dim pattern As String = "(\p{Sc}\s?)?(\d+\.?((?<=\.)\d+)?)(?(1)|\s?\p{Sc})?"
Dim input As String = "$17.43 €2 16.33 £0.98 0.43 £43 12€ 17"
Dim replacement As String = "$2"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)

the result is
Original String: '$17.43 €2 16.33 £0.98 0.43 £43 12€ 17'
Replacement String: '17.43 2 16.33 0.98 0.43 43 12 17'



in your example, it would be
pattern = "[<style(.*)</style>]"
input = 'raw html body
replacement = ">table.MsoNormalTable {font-size:1.5em;font-family:Arial,sans-serif;}div.MsoNormal {margin:0in;margin-bottom:.0001pt;font-size:1.5em;font-family:Arial,sans-serif;}.jim {font-size:5em;}@media only screen and (max-width:580){table.MsoNormalTable{font-size:3em;font-family:Arial,sans-serif;}}"

Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)
debug.print result

will need to tweak the pattern and replacement to get it right - it all depends where you put the () in the pattern.
Regex.Replace Method (String, String) (System.Text.RegularExpressions)
 
Status
Not open for further replies.
Top