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.
Thread starter Similar threads Forum Replies Date
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
R Add category via VBA in Outlook 2010 x64 not working? Exchange Server Administration 3
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
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
B Change Font and Font size using VBA Outlook VBA and Custom Forms 9
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
D.Moore VBA script fail after Office 365 update Using Outlook 8
R Limiting length of saved attachment in VBA Outlook VBA and Custom Forms 2
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
C How to use VBA to show only items x days old or more Outlook VBA and Custom Forms 1
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
S Reference Custom Fields with VBA Outlook VBA and Custom Forms 2
PGSystemTester VBA To Change AppointmentItem.BusyStatus From MeetingItem Before Send Using Outlook 0
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
G VBA Macro Calendar Printing Assistant 3
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
B VBA to Collapse Task Folder Groups Outlook VBA and Custom Forms 1
R Expand VBA Permanent Delete Code Outlook VBA and Custom Forms 6
shrydvd vba to secure zip attachments Outlook VBA and Custom Forms 3
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
N VBA to delete duplicates by message-id on common pst for 2 or more emails Outlook VBA and Custom Forms 0
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
A Custom VBA to sort emails into folders Outlook VBA and Custom Forms 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
R VBA for copying sent email to current folder under a shared mailbox Outlook VBA and Custom Forms 17
A Edit subject - and change conversationTopic - using VBA and redemption Outlook VBA and Custom Forms 2
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
N Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
B Clear Offline Items (Mail Folder) via VBA Outlook VBA and Custom Forms 1
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57
F VBA to ensure a code is entered in Subject title Outlook VBA and Custom Forms 1
B Vba to monitor time to respond to emails using a shared mailbox Outlook VBA and Custom Forms 5
Similar threads


















































Top