Sending HTML formatted email via Outlook from Access - ms-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

Related

Sending Outlook HTML email using Excel VBA generates runtime error

I'm trying to send a message from Excel. It triggers a run time error when I get to .HTMLBody.
Sub CreateHTMLMail()
Dim xOutApp As Outlook.Application
Dim xMailOut As Outlook.MailItem
Set xOutApp = CreateObject("Outlook.Application")
Set xMailOut = xOutApp.CreateItem(olMailItem)
With xMailOut
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY>Type the message text here. </BODY></HTML>"
.Display
End With
End Sub

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

Using CSS with .HTMLBody/olFormatHTML in Excel Generated Emails

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;">

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.

Interop.Outlook Message in HTML displayed as Plain Text

I recently switched from using EWS to using Interop.Outlook (see this article). The process is extremely easy to use!
Unfortunately, I have one problem that did not exist in EWS: Outlook does not process the HTML body even when BodyFormat is set to true. In this code sample (VB.NET), the MessageBody does start with < HTML. With debug, I verified that BodyFormat was set to HTML when display was executed. Nevertheless, the email body is displayed as plain text.
Dim Outlook As New Outlook.Application
Dim mail As Outlook.MailItem = DirectCast(Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem), Outlook.MailItem)
With mail
.To = Addr
.Subject = Subject
.Body = MessageBody
.BodyFormat = If(MessageBody.ToLower.StartsWith("<html"),
Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML,
Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatPlain)
.Display(Modal)
The exact same body text, when using EWS displays correctly.
.Body = MessageBody
The Body property of the MailItem class is a string representing the clear-text body of the Outlook item (without formatting). You need to set the body format first (if required). By default Outlook uses the HTML format.
With mail
.To = Addr
.Subject = Subject
If(MessageBody.ToLower.StartsWith("<html")) Then
.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML
.HTMLBody = MessageBody
Else
.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatPlain
.Body = MessageBody
End If
.Display(Modal)
Use the HTMLBody property for setting an HTML markup.
Or just simply:
With mail
.To = Addr
.Subject = Subject
If(MessageBody.ToLower.StartsWith("<html")) Then
.HTMLBody = MessageBody
Else
.Body = MessageBody
End If
.Display(Modal)