Create email with a htmlBody from file

Supposed is you have a html template or a htmlfile with all the formatting for the emailbody. We are going to add that htmlfile to the HTMLbody of an email.

1. Read the file into a string variable
2. Place that string variable in the body
On the Code VBA toolbar select Read File » Read File into String Variable

read file into stringvariable

The following code is inserted. Specify the filepath.


Sub CreateOutlook1EmailTextFile() 
Dim strFilename As String: strFilename = "C:\temp\testEmail.htm"
Dim strFileContent As String
Dim iFile As Integer: iFile = FreeFile
Open strFilename For Input As #iFile
strFileContent = Input(LOF(iFile), iFile)
Close #iFile

Now create the mailitem.


Dim mimEmail As Outlook.MailItem
Dim appOutlook As Object: Set appOutlook = CreateObject("Outlook.Application")
Dim strTo As String: strTo = Range(Cell1:="B5")
Dim strSubject As String: strSubject = "Invitation"
Set mimEmail = appOutlook.CreateItem(0)
With mimEmail
.To = strTo
.Subject = strSubject
.HTMLBody = strFileContent
.Display
End With
End Sub

Possible problems

not in the right encoding

This message can occur when the .htmlfile is not in the right encoding. To solve this, open in the Windows Explorer the htmlfile with Notepad en save it as ansi or utf-8

save as ansi

There are strange signs at the beginning of the resulting emailbody. This signs are the BOM (=byte-order mark), and also this is due to the encoding. Don't use 'UTF-8 with BOM', or make sure that that encoding is not set by mistake by the system.