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

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.

Related

How to include "mailto" in body of email?

I send an email through Outlook. I am trying to put an email address link within the body of the email, so that one can click on it and an email opens.
E.g. "If you have any questions, please contact us at ABC", where the "ABC" is the link that represents email address, ABC#gmail.com (not the real address).
I understand, from scouring the web, this is the syntax:
<a href="mailto:ABC#gmail.com\>ABC\</a>
I borrowed code that uses HTML to format a message in the body of an email, and replaced a line with what I wanted.
I received a Compile Error (which I think is actually a syntax error in this case?).
When debugging, the "mailto" text was highlighted.
My insertion:
ABC mailbox
.HTMLBody = "\<HTML\>\<BODY\>\<a href="mailto:ABC#gmail.com"\>ABC mailbox\</a>\</BODY\>\</HTML\>"
Sub SendEmailformattext()
'Update by Extendoffice.com
Dim xRg As Range
Dim xRgEach As Range
Dim xRgVal As String
Dim xAddress As String
Dim OutlookApp As Object
Dim MItem As Object
On Error Resume Next
xAddress = ActiveWindow.RangeSelection.Address
Set xRg = Application.InputBox("Please select email address range", "KuTools For Excel", xAddress, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set OutlookApp = CreateObject("Outlook.Application")
Set MItem = OutlookApp.CreateItem(olMailItem)
Set xRg = xRg.SpecialCells(xlCellTypeConstants, xlTextValues)
For Each xRgEach In xRg
xRgVal = xRgEach.Value
If xRgVal Like "?*#?*.?\*" Then
Set MItem = xOutApp.CreateItem(olMailItem)
With MItem
.Display
.To = xRgVal
.Subject = "Test"
.HTMLBody = "\<HTML\>\<BODY\>\<a href="mailto:ABC#gmail.com"\>ABC mailbox\</a>\</BODY\>\</HTML\>"
'.Send
End With
End If
Next
Set xMailOut = Nothing
UNCLASSIFIED
Set xOutApp = Nothing
Application.ScreenUpdating = True
End Sub
The original code worked so I know the mailto text is the problem.
Use the Chr(34) function if you need to use double quotes in the string:
.HTMLBody = "\<HTML\>\<BODY\>\**<a href="& Chr(34) & "mailto:ABC#gmail.com" & Chr(34) & "\>ABC mailbox\</a>**\</BODY\>\</HTML\>"

VBA Change the Font of the mail

I created a vba code but the font and the size of the mail are incorrect. I would like Calibri 11 but it's Calibri 10 for the first line (Hello) and Verdana 10 for the rest. How to proceed?
Sub mail_outlook()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.SentOnBehalfOfName = "xxxxxxxxxxxx"
.Display
.To = "xxxxxxxxxxxxxxxxxx"
.HTMLBody = "Hello, " & "<br>" & "<br>" & "Please find in attachment the Report." & "<br>" & "We remain available should you have any questions." & .HTMLBody
.CC = "xxxxxxxxxxxxxxx"
.BCC = ""
.Subject = "Report"
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You can specify the font size in the HTML:
"<font style=""font-family: Calibri; font-size: 11pt;""> your content goes here</font>"
See Change HTML email body font type and size in VBA for more information.
Also you may use the Word object model for doing that programmatically. The WordEditor of the Inspector class returns an instance of the Document class from the Word object model which represents the message body. See Chapter 17: Working with Item Bodies for more information. For example, in Word you could use the following sequence of property calls:
Selection.WholeStory
Selection.Font.Size = 12
Selection.Font.Name = "Georgia"

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

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