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.
Similar threads
Thread starter Title Forum Replies Date
M add new attendee to existing meetings with VBA Outlook VBA and Custom Forms 5
S Add VBA save code Using Outlook 0
J Execute Add-In Button from VBA Outlook 2016 Outlook VBA and Custom Forms 1
S Example VBA Macro - To Conditionally Change the From Account and Add a BCC Address on Emails Outlook VBA and Custom Forms 11
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
N Get the seconds with userproperties.add method VBA Outlook Outlook VBA and Custom Forms 4
O VBA to change message format and add formatted signature Outlook VBA and Custom Forms 1
B Auto BCC VBA macro: how to add exceptions? Using Outlook 28
J Add categories to Shared Mailbox using VBA Using Outlook 2
R Limiting meeting attendees using VBA or an add-in Using Outlook 9
P Creating Add-in from VBA Outlook VBA and Custom Forms 1
J Add Multiple Attachments Sources using VBA in Word Outlook VBA and Custom Forms 6
H add an appointment for a custom calendar, vba excel Outlook VBA and Custom Forms 2
R vba rule exception "except with specific words in the sender's add Outlook VBA and Custom Forms 2
K Add Hyperlink in Email Body by VBA Outlook VBA and Custom Forms 1
B convert VBA code to Add-in Outlook VBA and Custom Forms 1
J VBA for outlook to compare and sync between calendar Outlook VBA and Custom Forms 1
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
E Default shape via VBA Outlook VBA and Custom Forms 4
A Change settings Send/receive VBA Outlook VBA and Custom Forms 0
Z Import Tasks from Access Using VBA including User Defined Fields Outlook VBA and Custom Forms 0
E Outlook VBA change GetDefaultFolder dynamically Outlook VBA and Custom Forms 6
justicefriends How to set a flag to follow up using VBA - for addressee in TO field Outlook VBA and Custom Forms 8
D VBA code to select a signature from the signatures list Outlook VBA and Custom Forms 3
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S vba outlook search string with special characters Outlook VBA and Custom Forms 1
S VBA search string with special characters Outlook VBA and Custom Forms 1
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
K Outlook Office 365 VBA download attachment Outlook VBA and Custom Forms 2
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
N Save selected messages VBA does not save replies and/or messages that contain : in subject Outlook VBA and Custom Forms 1
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
V vBA for searching a cell's contents in Outlook and retrieving the subject line Outlook VBA and Custom Forms 1
B vBA for exporting excel file from outlook 2016 Outlook VBA and Custom Forms 3
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
L Need help modifying a VBA script for emails stuck in Outbox Outlook VBA and Custom Forms 6
K can't get custom form to update multiple contacts using VBA Outlook VBA and Custom Forms 3
S Excel vba code to manage outlook web app Using Outlook 10
H Custom Outlook Contact Form VBA Outlook VBA and Custom Forms 1
S Problem Checking the available stores in my Inbox (Outlook VBA) Outlook VBA and Custom Forms 0
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0

Similar threads

Top