Using CSS with .HTMLBody/olFormatHTML in Excel Generated Emails - html

I currently have a spreadsheet that I use to generate mass e-mails that I can format using HTML and it works really well. One thing that I have not been able to figure out is whether or not there is a way to also incorporate CSS styling within an HTML e-mail created through excel.
Has anyone tried this/had success with this before?
My current subroutine uses the Outlook Application to construct material into an HTML email. The code for calling/constructing is:
Sub Test_Email(what_address As String, subject_line As String, mail_body_message As String)
Dim olApp As Outlook.Application
Dim oAttatch As Outlook.Attachment
Set olApp = CreateObject("Outlook.Application")
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.To = what_address
.Subject = "My Subject"
.BodyFormat = olFormatHTML
.HTMLBody = mail_body_message
'mail_body_message is all text that resides in Cell A1 and it consists of body text of the email as well as the HTML tags that I use to organize the message display
.Display
End With
End Sub
Within cell A1, I have all of my message information. Starts with and ends with <HTML> </HTML> and then my message body resides within the tags. So far, when I have tried to insert CSS styles within the existing HTML in cell A1, it spits out the HTML just fine but treats the CSS as text.

You'll need to use inline CSS.
For example:
<img src="http://example.com/foo.jpg" style="max-width:100px;">
Or
<a href="http://example.com" style="color:#000;">

Related

Set a web link to a "word" in a VBA email

If this email does not display properly, please click here.
I would like to be able to have the person in the email click on the word "here" and then it will take them to a website.
This is what I have bee using but it just puts the full link into the email.
mymsg = "<HTML><BODY>"
mymsg = "If this email does not display properly, please click " & "<A href=http://us.localnews.com/ov?mailing=3TVGZMLJ-WDS49&m2u=3TVGZMLK-3TVGZMLJ-12Z630I>URL Text</A>"
mymsg = mymsg & "</BODY></HYML>"
Keep it simple, no need for the HTML/BODY tags
Example
Option Explicit
Public Sub Example()
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
Dim OutMail As Object
Set OutMail = OutApp.CreateItem(0)
With OutMail
.HTMLBody = "If this email does not display properly, please click " & _
"<A href=https://stackoverflow.com/>" & _
"Here</A>"
.Display
End With
End Sub
Make sure you are using HTMLBody Property
MSDN - HTMLBody Property
Returns or sets a String representing the HTML body of the specified item. The HTMLBody property should be an HTML syntax string. Read/write.

Code loses Outlook email template formatting

I am using this code in MS Access to to open a saved HTML Outlook Template. The code searches for "SALUTATION" in the body of the email and replaces it with controls data from my form.
It works pretty well but, I lose the email formatting which includes formatted text, multiple links and a few images.
How can my code be changed to keep the original formatting?
Private Sub Command139_Click()
Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem
Dim value As String
value = Me.Salutation & " " & Me.LastName
Set myOlApp = CreateObject("Outlook.Application")
Set MyItem = myOlApp.CreateItemFromTemplate("C:\Users\Meiaer\AppData\Roaming\Microsoft\Templates\ELMOVM.oft")
MyItem.Display
With MyItem
.To = Me.EMAIL_ADDRESS
MyItem.Body = Replace(MyItem.Body, "SALUTATION", value)
End With
Set MyItem = Nothing
Set myOlApp = Nothing
End Sub
Thank you
You need to edit MyItem.HTMLBody, not MyItem.Body (which is the plaintext representation of the mail body).
See https://technet.microsoft.com/en-us/library/ff868941(v=office.14).aspx

How to get rich text, which Access formats using HTML, into a Word doc

I am copying formatted text from a Word document into a rich text field in Access.
Later I want to use VBA to create a new Word document and write the text as formatted to it.
The problem is, Access saves rich text with HTML formatting. And when you try to write that to a doc or docx, you see the text and its HTML tags.
How do I write the text to a Word document so that it retains the intended formatting and doesn't show HTML codes?
The only way I have found that works (without modifying the input string) is to write the HTML to a temporary file, then use .InsertFile to load that file into the word document. Here's a sub that takes the input argument and puts it into a new Word document:
Sub WriteToWord(myHtmlFormattedText as String)
Dim objWord As Word.Application
Dim doc As Word.Document
Dim fso As Object ' FileSystemObject
Dim f As Object ' TextStream
Dim tempHtmlFile As String
' Write your HTML content to a temp file:
Set fso = CreateObject("Scripting.FileSystemObject")
tempHtmlFile = fso.GetSpecialFolder(2) & "\" & fso.GetTempName & ".htm"
Set f = fso.CreateTextFile(tempHtmlFile, True)
f.Write myHtmlFormattedText
f.Close
Set f = Nothing
Set fso = Nothing
' Set up word object
Set objWord = CreateObject("Word.Application")
With objWord
.Visible = True
Set doc = .Documents.Add
End With
'Add HTML file contents:
objWord.Selection.InsertFile tempHtmlFile
' Show the doc
doc.Activate
End Sub
Not sure this will help, but there are a few different pasting features in Microsoft Word (in thee upper left corner of the 'Home' tab) that many people overlook
This allows you to paste with/without formatting. Not sure if it will solve your specific issue, but hopefully!

Load HTML file into VBA Microsoft Access Email

I am trying to load an HTML file into an email that gets sent by my Microsoft Access database. The email gets sent when the user clicks a button (Command109)
Here is my code that sends the email:
Private Sub Command109_Click()
'Start of code
Dim strEmail, strBody As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
'Creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.Application")
DoEvents
Set objEmail = objOutlook.CreateItem(olMailItem)
DoEvents
'Creates string with email address
strEmail = PayeeEmail
strBody = "WHAT SHOULD I PUT HERE TO LOAD AN EXTERNAL HTML FILE?"
DoEvents
'Creates and sends email
With objEmail
DoEvents
.To = strEmail
DoEvents
.Subject = "Your Distribution from " & COMPANY & " has been processed."
DoEvents
.HTMLBody = strBody
DoEvents
DoEvents
.Send
End With
Set objEmail = Nothing
'Closes Outlook. Remove if you do not want to close Outlook
'objOutlook.Quit
Exit Sub
End Sub
I have this other code that allows me to load an HTML file into Outlook, but I'm not sure how to combine the code - so that the HTML file gets loaded into the BODY of the email being sent by Access.
Here is the code I have for a macro that will load an HTML file into Outlook:
Sub insertHTML()
Dim insp As Inspector
Set insp = ActiveInspector
If insp.IsWordMail Then
Dim wordDoc As Word.Document
Set wordDoc = insp.WordEditor
wordDoc.Application.Selection.InsertFile "C:\Users\me\Desktop\emailtemplate.html",
, False, False, False
End If
End Sub
Can anyone help me figure this out? Thank you for your time!
To concatenate strings on multiple lines you must have a space between the '&' and '_' as they are separate operators.
strBody = "<html><p> some of my html text here" & _ 'Note the spaces
"more formatted html text here" & _
"even more formatted html text here" & _
"don't forget your closing html brackets</p></html>"
with objEmail
.to = strTo
.subject = strSubject
.HTMKBody = strBody
.send
end with
For ease of readability in the code you can even create a seperate module with just the email string, just make sure it's public so that it can be called.
public strBody as string = your string here
I'm not sure about importing an HTML file directly, however in the past I have just placed the HTML code straight into the module. This is possible because you're using the .HTMLBody instead of .Body. You can also insert variable into the HTML code this way.
Straight HTML string
strBody = "<html> YOUR HTML CODE HERE </html>"
HTML using VBA variables
strBody = "<html><p> This is an email from " & COMPANY & ". We value your business</p></html>"
Obviously this isn't ideal if the template will change frequently. When I've done this in the past I've just made a template in outlook, copied the HTML code into VBA and then inserted variables where I wanted them.
There is likely a better way to do this though.

Sending HTML formatted email via Outlook from Access

Here is part of my code which works fine:
Dim objOutlook As New Outlook.Application
Dim objMail As MailItem
Dim Header as String
Dim Text As String
Dim EmailAddr as String
Set objOutlook = New Outlook.Application
Set objMail = objOutlook.CreateItem(olMailItem)
I then define Header, text and EmailAddr
With objMail
.To = EmailAddr
.Subject = Header
.Body = Text
.Send
End with
I want to format the text as Arial 12pt.
What do I insert to achieve this?
On using your suggestion I first get an email with out the test text and in font Calibri. If I try to send it, I get an error message. On SAVE, I find that I also have in Drafts, the version of the email as you predicted - correct font and text. How can I get rid of the first email? Also any hints on changing to bold and italic? Is it just a case of writing a normal html document and slotting it in the coding?
Based on a response to a thread here, it seems like the following might do the trick:
With objMail
.To = EmailAddr
.Subject = Header
.BodyFormat = olFormatHTML
.HTMLBody = "<span style=""font-family: Arial; font-size: 12pt;"">This is a test.</span>"
.Send
End With