Likes Likes:  0
Results 1 to 10 of 10

Thread: late binding for jpg hidden attachment embedded in outlook email body

  1. #1
    New Member
    Join Date
    Apr 2019
    Location
    Des Plaines, IL
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default late binding for jpg hidden attachment embedded in outlook email body

    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>

  2. #2
    Board Regular Worf's Avatar
    Join Date
    Oct 2011
    Location
    Rio, Brazil
    Posts
    3,592
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)

    Default Re: late binding for jpg hidden attachment embedded in outlook email body

    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
    Excel 2013 / Windows 8.1 (home)
    Excel 2013 / windows 7 (work)


  3. #3
    New Member
    Join Date
    Apr 2019
    Location
    Des Plaines, IL
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: late binding for jpg hidden attachment embedded in outlook email body

    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

  4. #4
    Board Regular Worf's Avatar
    Join Date
    Oct 2011
    Location
    Rio, Brazil
    Posts
    3,592
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)

    Default Re: late binding for jpg hidden attachment embedded in outlook email body

    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
    Excel 2013 / Windows 8.1 (home)
    Excel 2013 / windows 7 (work)


  5. #5
    Board Regular Worf's Avatar
    Join Date
    Oct 2011
    Location
    Rio, Brazil
    Posts
    3,592
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)

    Default Re: late binding for jpg hidden attachment embedded in outlook email body

    Excel 2013 / Windows 8.1 (home)
    Excel 2013 / windows 7 (work)


  6. #6
    New Member
    Join Date
    Apr 2019
    Location
    Des Plaines, IL
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: late binding for jpg hidden attachment embedded in outlook email body

    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.

  7. #7
    Board Regular bobsan42's Avatar
    Join Date
    Jul 2010
    Location
    Bulgaria, GMT+2 (42.891813,25.313594)
    Posts
    1,251
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    Default Re: late binding for jpg hidden attachment embedded in outlook email body

    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 by bobsan42; Apr 24th, 2019 at 01:55 PM.
    "...it's sad that in our blindness we gather thorns for flowers..."
    mostly using:
    windows 7 +10 (64-bit) / excel 2013 +2016 (32-bit) / access 2013 +2016 (32-bit) / some imagination & Google of course
    You don't need to read between the lines - just read them all!

  8. #8
    New Member
    Join Date
    Apr 2019
    Location
    Des Plaines, IL
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: late binding for jpg hidden attachment embedded in outlook email body

    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.

  9. #9
    New Member
    Join Date
    Apr 2019
    Location
    Des Plaines, IL
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: late binding for jpg hidden attachment embedded in outlook email body

    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

  10. #10
    Board Regular bobsan42's Avatar
    Join Date
    Jul 2010
    Location
    Bulgaria, GMT+2 (42.891813,25.313594)
    Posts
    1,251
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)

    Default Re: late binding for jpg hidden attachment embedded in outlook email body

    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
    "...it's sad that in our blindness we gather thorns for flowers..."
    mostly using:
    windows 7 +10 (64-bit) / excel 2013 +2016 (32-bit) / access 2013 +2016 (32-bit) / some imagination & Google of course
    You don't need to read between the lines - just read them all!

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •