Interop.Outlook Message in HTML displayed as Plain Text - html

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)

Related

Empty HTML Body while creating Outlook meeting invite using VBA

I'm trying to create an Outlook meeting invite.
The meeting invite is created. Subject and other fields are also updated.
I get an empty body when I use .HTMLBody.
Code I tried:
Set OutApp = Outlook.Application
Set OutMeet = OutApp.CreateItem(olAppointmentItem)
With OutMeet
.Subject = Subj
.RequiredAttendees = ActiveSheet.Range("G9").Value
ThisWorkbook.Sheets("Sheet1").Select
.Start = ThisWorkbook.Worksheets("Sheet1").Range("D2").Value
.Duration = 10
.Importance = olImportanceHigh
.ReminderMinutesBeforeStart = 15
ThisWorkbook.Sheets("Sheet2").Select
.BodyFormat = olFormatHTML
.HTMLBody = "<html><head></head><body>Hi</body></html>"
Application.Visible = True
MeetingStatus = olMeeting
.Location = "Microsoft Teams"
.Display
End With
Set OutMeet = Nothing
Set OutApp = Nothing
End Sub
There is no HTMLBody for appointment items.
See https://social.msdn.microsoft.com/Forums/en-US/1e7cc383-bd2f-4d18-8cd1-9489bb3011ea/getset-appointment-body-type-fromto-rtfhtml-plain-text?forum=outlookdev
Make sure you have your outlook reference libraries turned on and then try
.Body = "Hi"
This worked for me

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.

Embedding image in original size in HTML email

I am working with VBA through Access to create mail.
When I adjust an image size to less than the actual size, it embeds. When I omit size or put in as actual size, the image comes as an attachment.
Below is code snippet, miniplane.jpg comes as an attachment. Its actual size is 600x160. If I change in the code to 300x80 it shows.
Sub send_SHCmail()
'>>>> Declarations >>>>
Dim strPath As String
Dim strFileName As String
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
Dim olApp As Object
Dim objMail As Object
Set SHCData1 = Nothing
Set SHCData2 = Nothing
'>>>> Email Creation and Outlook Error Loop >>>>
On Error Resume Next 'Keep going if there is an error
Set olApp = GetObject(, "Outlook.Application") 'See if Outlook is open
If Err Then 'Outlook is not open
Set olApp = CreateObject("Outlook.Application") 'Create a new instance
End If
'Create e-mail item
Set objMail = olApp.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormathtml
.To = "no-reply#email.com"
.Bcc = SHCDistribution
.Sentonbehalfofname = "test#email.com"
.Subject = "Planning Report - " & Format(Now, "MMMM d, yyyy")
.Attachments.Add "\\local\Sdata\Logo-Facet-01.png", olByValue, 0
.Attachments.Add "\\local\Sdata\miniplane.jpg", olByValue, 0
.HTMLBody = "<!DOCTYPE html>"
'Body Header
.HTMLBody = .HTMLBody & "<html><head><body>"
.HTMLBody = .HTMLBody & "<img src=""cid:Logo-Facet-01.png"" alt=""Image Missing"" width=""215.6"" height=""96.53"" style=""display: block;"" />"
.HTMLBody = .HTMLBody & "<img src=""cid:miniplane.jpg"" alt=""Image Missing"" width=""600"" height=""160"" style=""display: block;"" />"
.Display
End With
End Sub
Firstly, don't use HTMLBody as an intermediate variable - reading and setting it is expensive. Use a local variable to build HTML , then set HTMLBody property only once.
If you are setting img tag through the src attribute, you must set the PR_ATTACH_CONTENT_ID property - see How to add images from resources folder as attachment and embed into outlook mail body in C#

Sending html email from VBA email program

I have written an email program for my organization the handles some very specialized things very well, things I could use Outlook or Gmail for. Now, the manager would like to send an occasional email to our small customer base, but I want the email body tto look professional and not send it as an attachment. I have cobbled together an html document that present in all browsers and has been validated. My problem is I can't figure out how to point the message body at the html document. Here is the salient code.
This is where all is set up:
Do While mailRs.EOF = False
'Me.AttachDoc = "C:\EmailFolder\CouponForm.pdf"
emTo = mailRs.Fields("EmailAddr").Value
emFrom = "SportsParkInfo#skokieparks.org"
emSubject = Me.Subject
emtextBody = Me.TextMessage
Here is a the call for sending the email
Call SendAMessage(emFrom, mailRs.Fields("EmailAddr").Value, _
emSubject, emtextBody, emAttach)
(I got the code for sending the email off the web and it works great through our mail server.)
In the above, before the call # emtextBody = Me.TextMessage is where I need to replace Me.TextMessage with the address/body of the html document. And the message box is a textBox on the ACCESS form. I can't find any control in ACCESS that takes html. I can't use the path to the html document because that generates an error. Is there a way of getting around this
If more information is required I'll be happy to supply it.
Thanks for your time.
jpl
Use something like the below code. I've included elements for attachment as well as html formatting but pretty much anything you can write in html can also be done within vba.
Sub SharePerformance()
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.createitem(0)
'& "\\server\folder" &
msg1 = "Team,<br><br><b><DL>" & Range("b5").Value & "</b><br><ul><b><u>" & Range("b6").Value & "</b></u>"
msg1 = msg1 & "<DT><a HREF=C:\USER\Desktop\File1.xlsb>"
msg1 = msg1 & Range("b7").Value & "</a><br>"
msg1 = msg1 & "<b><u>" & Range("b9").Value & "</b></u></DL><br><br>"
msg1 = msg1 & "<p><img src=file://" & "C:\temp\Chart1.png" & "></p>" & "<br>"
On Error Resume Next
' Change the mail address and subject in the macro before you run it.
With OutMail
.To = Range("B1").Value
.cc = ""
.BCC = ""
.Subject = Range("B3").Value
.HTMLBody = msg1
'.Attachments.Add ActiveWorkbook.FullName
'.Attachments.Add ("C:\temp\Chart1.png")
'.Attachments.Add ("C:\temp\Chart2.png")
.display
End With
SendKeys "^{ENTER}"
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I can't tell what code is inside that SendAMessage function you are using but all the VBA examples I've worked with seem to work the same way with the CDO.Message object like in this MS Knowledge Base article KB286431. At some point SendAMessage is going to have a line that assigns the message object's .TextBody value to be equal to the emtextBody parameter you pass in.
One solution may be to copy your SendAMessage function into a new function SendAMessageHTML and replace the line where they are setting someMessage.TextBody = emtextBody so that you are setting someMessage.HTMLBody = emtextBody
Assuming your textbox has text along the lines of "<html><head><body></body></html>" you could modify your existing function to do a naive check like this:
if Left(UCase(emtextBody),6) = "<HTML>" then
someMessage.HTMLBody = emtextBody
else
someMessage.TextBody = emtextBody
end if

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