VBA Change the Font of the mail - html

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"

Related

How to attach an HTML jpeg to email?

I utilize Excel to mass mail clients. My VBA code can't locate the HTML jpeg for the advertisement.
Is the imgur link not good or is it my code?
MailWindows()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "insert here"
.Attachments.Add Fname, 1, 0
.HTMLBody = "<img src=""cid:"https://url.jpeg height=1650 width=1275>"
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
Besides the quote problems, you are referring to an image through cid (content-id), which means the email client must look for an attachment in the same message with the Content-ID MIME header set to https://url.jpeg.
If you meant to refer to an external image, it needs to be
.HTMLBody = "<img src=""https://url.com/someimage.jpeg"" height=1650 width=1275>"
If you refer to an embedded image, you need to actually add the attachment and set PR_ATTACH_CONTENT_ID MAPI property appropriately using Attachment.PropertyAccessor.SetProperty

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.

How to remove the automatically generated lines above signature in HTML body?

When I .Display mail, to add the signature, two empty lines are added above the signature. Is there a way to remove them, to make the mail look better, without losing the signature formatting?
With objOutlookMsg
.SentOnBehalfOfName = "test#stackoverflow.com"
.To = "test#stackoverflow.com"
.CC = "test#stackoverflow.com"
.Subject = "Stackoverflow"
' Tekst
.Display
.HTMLBody = "<p style='font-family:arial;font-size:13'>" & _
"Hej" & "</p>" & _
vbNewLine & vbNewLine & _
"<p style='font-family:arial;font-size:13'>" & _
"Test Test Test Test Test Test" & "</p>" & _
.HTMLBody
.Display
End With
Picture of mail and signature to show, that there are no empty lines at the beginning.
It's been a few years since you asked this, I just struggled with the same issue, where I wanted the signature to work regardless of who is sending. I figured I would still share here in case anyone else is having similar issues.
I'm not a great coder, but I was able to piece things together through a bunch of different articles/posts to get something working, so there is likely a more efficient way to execute this.
To get the signature, it effectively displays an empty e-mail with default signature, removes the white space, saves it, and then empties and discards the e-mail before moving to the final e-mail send (on my comp just discarding left the signature at the top of the final email). The signature is attached at the end of that e-mail, .HTMLBody is used for both instances.
Here is the full code with 'default' inputs, for anyone else that may stumble across:
Sub sig_and_email_send()
Dim OutApp As Object
Dim OutMail As Object
Dim signature As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'gets default signature from e-mail
With OutMail
'2 = HTMLBody Format
.BodyFormat = 2
.Display
'deletes blank space present before signature
signature = Replace(OutMail.HTMLBody, "<p class=MsoNormal><o:p> </o:p></p>", "")
'removes entire e-mail contents and then closes with discard
OutMail.HTMLBody = Replace(OutMail.HTMLBody, OutMail.HTMLBody, "")
OutMail.Close 1
End With
On Error Resume Next
With OutMail
.to = "email#email.com"
.CC = "email#email.com"
.BCC = "email#email.com"
.Subject = "Subject"
.Attachments.Add "\\network\folder\documents\file.xlsx"
.HTMLBody = .HTMLBody & "Hi," & "whateveryourtextis" & _
"<br>" & "Thanks," & "<br><br>" & signature
.Display 'or use .Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

VBA Excel Email Formatting Issue

My data is set up like this:
-On Excel, I have a worksheet, "Test1", which has a list of people I want to send emails to. Like such;
No. Version Company Name Email
1 R x Max max#abc.com
2 E y Bill bill#abc.com
3 C z Scott scott#abc.com
The emails I want to send differ slightly by type. My current code is as follows:
Sub mailTest()
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
For i = 2 To 3
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
mailBody = Worksheets("BODY").Cells(1, 1).Value & Worksheets("Test1").Cells(i, 4) _
& Worksheets("BODY").Cells(2, 1).Value
With olMail
.To = Worksheets("Test1").Cells(i, 5).Value
.Subject = Worksheets("Test1").Cells(i, 3).Value
.HTMLBody = mailBody
.Display
End With
Set olMail = Nothing
Set olApp = Nothing
Next
End Sub
The code above displays the emails as the main body is in Cell A2 of sheet "BODY". The "Hello" is stored in Cell A1, and also displays fine as I have HTML coding in both these cells to set the font to Calibri and the size to 3.
The issue is when I'm pulling the names of people from sheet Test1 in Column 4 using the code;
& Worksheets("Test1").Cells(i, 4)
It is displaying with a different font, defaulting to Times New Roman. What I'm left with is everything displaying in Calibri, with just their names being pulled in Times New Roman.
Is there any way to fix this without adding HTML code in each of the name cells? The actual file I'm working on has about 500 names so manually doing it would be a pain...
Thanks everyone.
Try replace
.HTMLBody = mailBody
with
.HTMLBody = "<!DOCTYPE html><html><head><style>"
.HTMLBody = .HTMLBody & "body{font-family: Calibri, ""Times New Roman"", sans-serif}"
.HTMLBody = .HTMLBody & "</style></head><body>"
.HTMLBody = .HTMLBody & mailBody & "</body></html>

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#