Access Email with Embedded Image Using Outlook (1 Viewer)

durin

New member
Local time
Today, 17:20
Joined
Sep 21, 2013
Messages
6
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 but the code needs to work with later versions of Access and Outlook.

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

Ideally I want to take the image from an attachment field in a table returned by the "tblMailingList_Query".

I have attached my current code and any help would be greatly appreciated.

Code:
[FONT=Calibri]Private Sub Command10_Click()[/FONT]
 
[FONT=Calibri] Dim MyDB As Database[/FONT]
[FONT=Calibri] Dim MyRS As Recordset[/FONT]
[FONT=Calibri] Dim objOutlook As Outlook.Application[/FONT]
[FONT=Calibri] Dim objOutlookMsg As Outlook.MailItem[/FONT]
[FONT=Calibri] Dim objOutlookRecip As Outlook.Recipient[/FONT]
[FONT=Calibri] Dim objOutlookAttach As Outlook.Attachment[/FONT]
[FONT=Calibri] Dim TheAddress As String[/FONT]
 
[FONT=Calibri] On Error Resume Next[/FONT]
 
[FONT=Calibri] Set MyDB = CurrentDb[/FONT]
[FONT=Calibri] Set MyRS = MyDB.OpenRecordset("tblMailingList_Query")[/FONT]
[FONT=Calibri] MyRS.MoveFirst[/FONT]
 
[FONT=Calibri] ' Create the Outlook session.[/FONT]
[FONT=Calibri] Set objOutlook = CreateObject("Outlook.Application")[/FONT]
 
[FONT=Calibri] Do Until MyRS.EOF[/FONT]
[FONT=Calibri] ' Create the e-mail message.[/FONT]
[FONT=Calibri] Set objOutlookMsg = objOutlook.CreateItem(olMailItem)[/FONT]
[FONT=Calibri] TheAddress = MyRS![EAddress][/FONT]
 
[FONT=Calibri]    With objOutlookMsg[/FONT]
[FONT=Calibri]       ' Add the To recipients to the e-mail message.[/FONT]
[FONT=Calibri]       Set objOutlookRecip = .Recipients.Add(TheAddress)[/FONT]
[FONT=Calibri]       objOutlookRecip.Type = olTo[/FONT]
 
[FONT=Calibri]       ' Add the Cc recipients to the e-mail message.[/FONT]
[FONT=Calibri]       If (IsNull(Forms!frmMail!CCAddress)) Then[/FONT]
[FONT=Calibri]       Else[/FONT]
[FONT=Calibri]          Set objOutlookRecip = .Recipients.Add(Forms!frmMail!CCAddress)[/FONT]
[FONT=Calibri]          objOutlookRecip.Type = olCC[/FONT]
[FONT=Calibri]       End If[/FONT]
 
[FONT=Calibri]       ' Set the Subject, the Body, and the Importance of the e-mail message.[/FONT]
[FONT=Calibri]       .Subject = Forms!frmMail!Subject[/FONT]
[FONT=Calibri]       .Body = Forms!frmMail!MainText & vbCrLf & vbCrLf & Forms!frmMail!SecondText[/FONT]
[FONT=Calibri]       .Importance = olImportanceHigh  'High importance[/FONT]
 
[FONT=Calibri]       ' Resolve the name of each Recipient.[/FONT]
[FONT=Calibri]       For Each objOutlookRecip In .Recipients[/FONT]
[FONT=Calibri]          objOutlookRecip.Resolve[/FONT]
[FONT=Calibri]          If Not objOutlookRecip.Resolve Then[/FONT]
[FONT=Calibri]            objOutlookMsg.Display[/FONT]
[FONT=Calibri]          End If[/FONT]
[FONT=Calibri]       Next[/FONT]
[FONT=Calibri]       .Send[/FONT]
[FONT=Calibri]     End With[/FONT]
[FONT=Calibri]     MyRS.MoveNext[/FONT]
[FONT=Calibri]  Loop[/FONT]
[FONT=Calibri]  Set objOutlookMsg = Nothing[/FONT]
[FONT=Calibri]  Set objOutlook = Nothing[/FONT]
[FONT=Calibri][FONT=Calibri]End Sub[/FONT]
[/FONT]
 

DavidAtWork

Registered User.
Local time
Today, 10:20
Joined
Oct 25, 2011
Messages
699
If the image is to be the same for each email, then you could solve this simply by using a template and instead of
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
use
Set objOutlookMsg = objOutlook.CreateItemFromTemplate(olMailItem)
where olMailItem is a variable that holds the location/name of the template file
but I suspect you want to imbed a different image with each email

David
 

durin

New member
Local time
Today, 17:20
Joined
Sep 21, 2013
Messages
6
Hi David

Yes you are right, I ideally want the image for each email to be extracted from the query (MyRS!) the same as the email address is, so it would be different for each email

Code:
[FONT=Calibri]TheAddress = MyRS![EAddress][/FONT]

The address seems very easy to define, obviously an embedded image is not ?

e.g it would be nice if it was as simple as

EmbeddedObject = MyRS![Image1]

Ha ha

Don't know if you have any other ideas ?

thanks

Durin
 

Users who are viewing this thread

Top Bottom