PDA

View Full Version : Send Outlook Email with Embedded Picture



durobell
09-13-2013, 06:16 AM
Hi, I am brand new to the forum and was looking for some help with some code to automatically send emails out from an Access Customer Contacts Database. I am using Access and Outlook 2007.

I have very poor knowledge of coding and usually manage to cobble something together from looking at other code on the net but don't understand most of it.

I have the following code which works perfectly (sends emails from a form with a couple of text fields and a send button) except I want to be able to embed an image in the email body (not have the image as an attachment but actually show it in the body of the email). Most of the code I have found around this topic is too complex for me to understand and utilise within the context of the code I have.

I also noted that most examples showed using an image from the C drive where I want to use an image contained within a table "tblMailingList" (as an attachment field) and returned in the tblMailingList_Query in the 'Image' field.

I have included my current code below and any help (assume I understand very little about the code) would be greatly appreciated.




Private Sub Command10_Click()

Dim MyDB As Database
Dim MyRS As Recordset
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim TheAddress As String

Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset("tblMailingList_Query")
MyRS.MoveFirst

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")

Do Until MyRS.EOF
' Create the e-mail message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
TheAddress = MyRS![EmailAddress]

With objOutlookMsg
' Add the To recipients to the e-mail message.
Set objOutlookRecip = .Recipients.Add(TheAddress)
objOutlookRecip.Type = olTo

' Add the Cc recipients to the e-mail message.
If (IsNull(Forms!frmMail!CCAddress)) Then
Else
Set objOutlookRecip = .Recipients.Add(Forms!frmMail!CCAddress)
objOutlookRecip.Type = olCC
End If

' Set the Subject, the Body, and the Importance of the e-mail message.
.Subject = Forms!frmMail!Subject
.Body = Forms!frmMail!MainText & vbCrLf & vbCrLf & Forms!frmMail!SecondText
.Importance = olImportanceHigh 'High importance

' Resolve the name of each Recipient.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
End If
Next
.Send
End With
MyRS.MoveNext
Loop
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub

mrojas
09-16-2013, 09:09 PM
Try this line of code, the one with the '==============. Other lines of code are for you to figure out where you should insert it.

' Set the Subject, the Body, and the Importance of the e-mail message.
.Subject = Forms!frmMail!Subject
.Body = Forms!frmMail!MainText & vbCrLf & vbCrLf & Forms!frmMail!SecondText
.Importance = olImportanceHigh 'High importance
'====== New line of code follows =============
.HTMLBody = "<img src=cid:" & Replace(MyPicture, " ", "%20") & " height=240 width=180>"
'=====================================

You need to replace the MyPicture variable with the path of where the image resides.

durobell
09-17-2013, 03:32 AM
Hi thanks for your help

I ideally want to take the image from the database itself but at this stage I just used a direct path to an image on the hard drive of the pc. The email got sent but all that was visible was a square with the red cross in the top corner ? Even the text that used to work is no longer visible.