Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.HTMLBody Image question

Status
Not open for further replies.

OrbitMascot

Programmer
Jan 23, 2006
30
US
This is a question concerning inserting an image into an email that is sent from Access 97.

Listed below is the current code that is used to produce the email. Everything works fine, except the email just shows a graphic box in the email not the actual graphic that is needed to be seen.

I have been doing some research on the problem and have yet to come up with any solutions. Any information that anyone could provide would be greatly appreciated. Thanks for all of you help.

ndBodyShop = "<html><body><img scr=""U:\data\distribution\invoices\logo.jpg""><body><html>" & "<p><font face=Arial color=blue size=8>**DATE CHANGE**</font>" & "<br><p><font face=Arial size=4> One of our New Shops has a new delivery date, please refer to the information below: <br><p>" & "<p><font face=Arial size=6>Store: " & [newstore] & "<p><font face=Arial size=6>Shop: " & [newshop] & "<p><font face=Arial size=6>Old Ship Date: " & [olddate] & "<p><font face=Arial size=6>New Ship Date: " & [newdate]


With objEmailShop
.To = ndEmailShop
.Subject = "UPDATE on New Shop Setup"
.HTMLBody = ndBodyShop
.Send
End With

 
you need to attach the image to the mime header of the email and then reference it via its CID.
Code:
' Outlook objects
  Dim objApp As Outlook.Application
  Dim l_Msg As MailItem
  Dim colAttach As Outlook.Attachments
  Dim l_Attach As Outlook.Attachment
  Dim oSession As MAPI.Session
  ' CDO objects
  Dim oMsg As MAPI.Message
  Dim oAttachs As MAPI.Attachments
  Dim oAttach As MAPI.Attachment
  Dim colFields As MAPI.Fields
  Dim oField As MAPI.Field
  Dim sHTML As String
  Dim sHead As Image
  
    
  Dim strEntryID As String
  
  ' create new Outlook MailItem
  Set objApp = CreateObject("Outlook.Application")
  Set l_Msg = objApp.CreateItem(olMailItem)
  ' add graphic as attachment to Outlook message

  Set colAttach = l_Msg.Attachments
  Set l_Attach = colAttach.Add("C:\myimage.gif")
  
  'get MSG ID
  strEntryID = l_Msg.EntryID
  
  'clear mail item variables
  Set l_Msg = Nothing
  
  ' *** POSITION CRITICAL *** you must dereference the
  ' attachment objects before changing their properties
  ' via CDO
  Set colAttach = Nothing
  Set l_Attach = Nothing
    
  ' initialize CDO session

  Set oSession = CreateObject("MAPI.Session")
  oSession.Logon "", "", True, False
  
  ' get the message created earlier
  Set oMsg = oSession.GetMessage(strEntryID)
  ' set properties of the attached graphic that make
  ' it embedded and give it an ID for use in an <IMG> tag
  
  '1st Image - repeat as necessary
  Set oAttachs = oMsg.Attachments
  Set oAttach = oAttachs.Item(1)
  Set colFields = oAttach.Fields
  Set oField = colFields.Add(CdoPR_ATTACH_MIME_TAG, "image/gif")
  Set oField = colFields.Add(&H3712001E, "image1")
  oMsg.Fields.Add "{0820060000000000C000000000000046}0x8514", 11, True
  oMsg.Update
    
  ' get the Outlook MailItem again
  Set l_Msg = objApp.GetNamespace("MAPI").GetItemFromID(strEntryID)
  
' add HTML content --
sHTML = "<img src=""cid:image1"">"

'set body EQ HTML
l_Msg.HTMLBody = sHTML

'set email subject
l_Msg.Subject = rs.Fields("sSubject")

l_Msg.Send


  ' clean up objects
  Set oField = Nothing
  Set colFields = Nothing
  Set oMsg = Nothing
  oSession.Logoff
  Set oSession = Nothing
  Set objApp = Nothing
  Set l_Msg = Nothing

remeber not to use too many images as it bloats the email, but at least you don't need to host the images or the recipient connected to internet to view it!

note: i've hacked my code apart for this example, might have an error in it, so post back with what you have if you need further help.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top