late binding for jpg hidden attachment embedded in outlook email body

MPoppins

New Member
Joined
Apr 15, 2019
Messages
5
Hello, I have been doing a lot of searching, but I am struggling to find the code when late binding to embed a jpeg image in an outlook email body, while it is hidden in the attachments. I've tried numerous configurations online, my current code gives me an error of "array index out of bounds" at the .display line.

Here is my code:

'Dim Email variables
Dim objEmail As Object
Dim objApp As Object
Dim strContact As String
Dim strSendTo As String
Dim strSubject As String
Dim rCount as Integer
Dim WksBidLst as Worksheet


'rCount is for looping - I've excluded the loop references as that is working fine.

'Assign Email Variables

rCount = 12
strContact = WksBidList.Range("H" & rCount)
strSendTo = WksBidList.Range("K" & rCount)
strSubject = "Invitation to Bid " & WksBidList.Range("E1")
strUserName = WksBidList.Range("F" & rCount)
strPassword = WksBidList.Range("G" & rCount)
strCategory = WksBidList.Range("B" & rCount)



'Get Logo jpeg
Dim Strbody as string
Dim StrITBLogoFilePath As String
Dim ObjITBLogo As Object
Dim StrITBLogo As String
StrITBLogoFilePath = "M:\Preconstruction\DO NOT MOVE OR EDIT\ITB Files"
Debug.Print StrITBLogoFilePath
Set ObjITBLogo = CreateObject("Scripting.FileSystemObject").OpenTextfile(StrITBLogoFilePath & "image001.jpg")
Let StrITBLogo = ObjITBLogo.readall
ObjITBLogo.Close



'Body of Email
Strbody = "<src=cid:image001>" & StrITBLogoFilePath & "
" & "Dear.... "


'Create Email Item (NOTE 2nd attachment below works fine, didn't include sourcing for that)

Set objApp = CreateObject("Outlook.Application")
Set objEmail = objApp.CreateItem(0)

With objEmail
.To = strSendTo
.Subject = strSubject
.HTMLBody = Strbody
.attachments.Add StrITBLogoFilePath & "image001.jpg", 0
.attachments.Add (Application.ActiveWorkbook.Path & StrAttachSummary)
.Save
.Display
End With


'Clear Objects
Set objEmail = Nothing
Set objApp = Nothing
Set ObjDefaultSignature = Nothing

end Sub</src=cid:image001>
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to the Board

I successfully tested the below code with Outlook 2007. What Office version are you using?

Code:
' Outlook 2007 module
Sub EmbeddedHTMLGraphic()
' add reference to Microsoft CDO 1.21 Library
Dim objApp As Application, l_Msg As MailItem, colAttach As Attachments, att As attachment, ses As MAPI.Session
Dim oMsg As MAPI.Message, oAttachs As MAPI.Attachments, oAttach As MAPI.attachment, colFields As MAPI.Fields
Dim oField As MAPI.Field, eid$
Set objApp = CreateObject("Outlook.Application")
Set l_Msg = objApp.CreateItem(olMailItem)
Set colAttach = l_Msg.Attachments ' add graphic as attachment to Outlook message
Set att = colAttach.Add("c:\pub\uf.jpg")   ' your path here
l_Msg.Close olSave
eid = l_Msg.EntryID
Set l_Msg = Nothing
' you must dereference the attachment objects before changing their properties via CDO
Set colAttach = Nothing
Set att = Nothing
Set ses = CreateObject("MAPI.Session") ' initialize CDO session
ses.Logon "", "", False, False
Set oMsg = ses.GetMessage(eid) ' get the message created earlier
' set properties of the attached graphic that make it embedded and give it an ID for use in an <IMG> tag
Set oAttachs = oMsg.Attachments
Set oAttach = oAttachs.item(1)
Set colFields = oAttach.Fields
Set oField = colFields.Add(CdoPR_ATTACH_MIME_TAG, "image/jpeg")
Set oField = colFields.Add(&H3712001E, "myident")
oMsg.Fields.Add "{0820060000000000C000000000000046}0x8514", 11, True
oMsg.Update
Set l_Msg = objApp.GetNamespace("MAPI").GetItemFromID(eid) ' get the MailItem again
l_Msg.HtmlBody = "<IMG align=baseline border=0 hspace=0 src=cid:myident>" ' add HTML content -- the <IMG> tag
l_Msg.Close (olSave)
l_Msg.Display
Set oField = Nothing:  Set colFields = Nothing:  Set oMsg = Nothing
ses.Logoff
Set ses = Nothing:  Set objApp = Nothing: Set l_Msg = Nothing
End Sub
 
Upvote 0
I'm in office 365, but others in my office are using 2013, and I need to make this program compatible with their system as well, hence the late binding. I've determined that I need to first attach the jpg as an attachment, with the location value of 0 so that it is hidden, then attach to the body of the email as an embed. Its the location value of zero and the embedded attachment lines of code that are going awry I believe. I can't find anything that spells out the correct syntax for a late binding process.


Thanks, jen
 
Upvote 0
Does this work?

Code:
' Excel module
Sub mail()
Dim myitem, olapp
Set olapp = CreateObject("Outlook.Application")
Set myitem = olapp.CreateItem(olMailItem)
With myitem
    .To = "marypoppins@fan.net"
    .Subject = "Umbrellas"
    .Body = "body"
    .Attachments.Add "C:\pub\uf.jpg", olByValue, 0
    .HTMLBody = "******><IMG src=""cid:uf.jpg"" width=200> </BODY>"
    .Display
End With
Set myitem = Nothing
Set olapp = Nothing
End Sub
 
Upvote 0
x31RnqV.jpg
 
Upvote 0
Thanks, but no, it doesn't. That is an early binding code, which would work if everyone in my office was using the same version of excel, but unfortunately they are not, which means they reference different object libraries than I do, which causes the code to fail. So... I need to use late binding, where I assign the variable to an object, and then set the object equal to an email item, etc. Changes the syntax enough to make it confusing.
 
Upvote 0
I think this is not exactly the idea of early or late binding. And I don't think this would really matter much.
Early or late binding is irrelevant in many ways - for me it is much easier writing the code binding early and making all references explicit, then if necessary I change it to late and the references implicit.
I would suggest making the code work first with early binding with the correct references - this will help by exposing methods, properties etc.After you make it work, then worry about binding and compatibility.

And a small correction the code provided above is not early binding AFAIK - the fact that an object is set on the line does not make it early.
 
Last edited:
Upvote 0
I did make it work with early binding on my computer, but then doesn't work on others' b/c it cannot find the object b/c we have different object libraries with the different excel versions.
 
Upvote 0
I had an error when i tried your code the first time, but upon identifying that and making the change the code you provided does in fact work! My apologies for not recognizing this previously - many thanks!! -MP
 
Upvote 0
Sorry, I was partially right. The code from Worf in Post 4 (and 5) is a late-binding code, with two small mistakes.
The code works perfectly in Outlook 2016 (I believe will also work in Outlook 2013-2010).
However to make it full late binding compatible and to work in Excel w/o additional references two small changes are required:
change olMailItem to 0
change olByValue to 1
And the code does exactly what you need:
Code:
option explicit 

Sub mail222()
    Dim myitem, olapp
    Set olapp = CreateObject("Outlook.Application")
    Set myitem = olapp.CreateItem(0)
        With myitem
            .To = "marypoppins@fan.net"
            .Subject = "Umbrellas"
            .Body = "body"
            .Attachments.Add "C:\pub\uf.jpg", 1, 0
            .HTMLBody = "<IMG src=""cid:uf.jpg"" width=200> "
            .Save
            .Display
        End With
    Set myitem = Nothing
    Set olapp = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,188
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top