How to attach an HTML jpeg to email? - html

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

Related

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"

Access Send Email with Attachment [duplicate]

I have some code that creates a Mail object (Outlook), attaches a file and sends it.
Dim mobjOutlook, mobjActiveExp, mobjNewMail As Object
'Create Outlook objects
Set mobjOutlook = CreateObject("Outlook.Application")
Set mobjActiveExp = mobjOutlook.ActiveExplorer
Set mobjNewMail = mobjOutlook.CreateItem(olMailItem)
'Setup and send email
With mobjNewMail
.To = "someone#test.com"
.Subject = "theSubject"
.Body = "some text"
.Attachments.Add "C:/The/File/Path.doc"
'*I need to check here if the above line worked*
.Send
End With
How can I test if the attachment works before sending? Is this possible? For some reason even if it doesn't, it still sends the email without the attachment.
I was thinking of somehow utilizing the '.Save' option.
Any thoughts or suggestions are much appreciated,
thanks.
You could just test the number of attachments in the email were > 0
Also
Dim mobjOutlook, mobjActiveExp, mobjNewMail As Object
will dim the first two variables as variants, so I have recut this below
Sub Test()
Dim mobjOutlook As Object
Dim mobjActiveExp As Object
Dim mobjNewMail As Object
'Create Outlook objects
Set mobjOutlook = CreateObject("Outlook.Application")
Set mobjActiveExp = mobjOutlook.ActiveExplorer
Set mobjNewMail = mobjOutlook.CreateItem(olMailItem)
'Setup and send email
With mobjNewMail
.To = "someone#test.com"
.Subject = "theSubject"
.Body = "some text"
.attachments.Add "C:\temp\step1.png"
If .attachments.Count > 0 Then
.Send
Else
MsgBox "No attachment", vbCritical
End If
End With
End Sub

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#

Getting Access to send emails to dynamic addresses

This is in Access 2010 and I have virtually no experience or familiarity with VBA.
In my form (frmEmailLookup), I have combo boxes and list boxes and a subform set up so that when the user selects a building from cmbBuilding the remainder of the form populates with the data on that building, including the contact emails for up to 4 people in the building (lstBuildingRepEmail1, lstBuildingRepEmail2, lstBuildingRepEmail3, lstBuildingRepEmail4). I need a button (butEmailRecords) to generate an email with the query from the subform (qryBuildingAreaLookup) as an attachment. I can set up a macro that will something close, but it doesn't allow for dynamic email addresses. I don't want my users to have to go that far into the program to make updates.
Any help is appreciated and I know I'm asking for a lot of code writing help.
Here's what I've tried:
Option Compare Database
Private Sub butEmailRecords_Click()
Dim outputFileName As String
outputFileName = CurrentProject.Path & "\BuildingInventory" & ".xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryBuildingAreaLookup", outputFileName, True
On Error GoTo Error_Handler
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("qryBuildinAreaLookup")
With rs
With objEmail
.To = tblBuilding.BuildingRep1
.To = tblBuilding.BuildingRep2
.To = tblBuilding.BuildingRep3
.To = tblBuilding.BuildingRep4
.Subject = "Look at this sample attachment"
.body = "The body doesn't matter, just the attachment"
.Attachments.Add "L:\Administration\FacilityInventoryDatabase\BuildingInventory.xls x"
.Send
'.ReadReceiptRequested
End With
Exit_Here:
Set objOutlook = Nothing
Exit Sub
Error_Handler:
MsgBox Err & ": " & Err.Description
Resume Exit_Here
End Sub
Here is the basics of what I use:
'Refers to Outlook's Application object
Dim appOutlook As Object
'Refers to an Outlook email message
Dim appOutlookMsg As Object
'Refers to an Outlook email recipient
Dim appOutlookRec As Object
'Create an Outlook session in the background
Set appOutlook = CreateObject("Outlook.Application")
'Create a new empty email message
Set appOutlookMsg = appOutlook.CreateItem(olMailItem)
'Using the new, empty message...
With appOutlookMsg
'SQL statement to grab emails
Set recordset = currentdb.openrecordset('SQL statement')
Do While Not recorset.EOF
Set appOutlookRec = .Recipients.Add(recordset.Email)
appOutlookRec.Type = olTo
recordset.MoveNext
Loop
.Subject = ....
.Body = ....
.Send
End With
and that's the basics of what I use. I'm a beginner so this may not be the best way, but it should be a start. (I also had to add Microsoft Oulook in the reference library.)
I use CDO objects to send messages because I prefer not to rely on Outlook (for anything).
There is quite a comprehensive article on using CDO to send mail (including downloadable VBA code) here:
http://www.cpearson.com/excel/Email.aspx

How to check if .Attachment.Add "filename" is successful before send

I have some code that creates a Mail object (Outlook), attaches a file and sends it.
Dim mobjOutlook, mobjActiveExp, mobjNewMail As Object
'Create Outlook objects
Set mobjOutlook = CreateObject("Outlook.Application")
Set mobjActiveExp = mobjOutlook.ActiveExplorer
Set mobjNewMail = mobjOutlook.CreateItem(olMailItem)
'Setup and send email
With mobjNewMail
.To = "someone#test.com"
.Subject = "theSubject"
.Body = "some text"
.Attachments.Add "C:/The/File/Path.doc"
'*I need to check here if the above line worked*
.Send
End With
How can I test if the attachment works before sending? Is this possible? For some reason even if it doesn't, it still sends the email without the attachment.
I was thinking of somehow utilizing the '.Save' option.
Any thoughts or suggestions are much appreciated,
thanks.
You could just test the number of attachments in the email were > 0
Also
Dim mobjOutlook, mobjActiveExp, mobjNewMail As Object
will dim the first two variables as variants, so I have recut this below
Sub Test()
Dim mobjOutlook As Object
Dim mobjActiveExp As Object
Dim mobjNewMail As Object
'Create Outlook objects
Set mobjOutlook = CreateObject("Outlook.Application")
Set mobjActiveExp = mobjOutlook.ActiveExplorer
Set mobjNewMail = mobjOutlook.CreateItem(olMailItem)
'Setup and send email
With mobjNewMail
.To = "someone#test.com"
.Subject = "theSubject"
.Body = "some text"
.attachments.Add "C:\temp\step1.png"
If .attachments.Count > 0 Then
.Send
Else
MsgBox "No attachment", vbCritical
End If
End With
End Sub