Embed multiple pictures in Outlook Email

hk1

Registered User.
Local time
Today, 12:05
Joined
Sep 1, 2009
Messages
121
I found this code to embed one email in an outlook email using VB or VBA.
http://www.outlookcode.com/d/code/htmlimg.htm

I also found this discussion but it didn't help me out very much since I could find the complete code anywhere as a final solution:
http://www.outlookcode.com/threads.aspx?forumid=4&messageid=14726

I'm wondering if anyone can help me out. I need to embed multiple images and the quantity will vary.

I also need to use late binding instead of early binding, if at all possible. We have different versions of Outlook within our organization.

Last but not least, I'd prefer to use Outlook Redemption Objects for this entire task but I don't know how to port this code to use Outlook Redemption Objects. If you can help me out with my first two questions, that would be great. Redemption Objects is not a necessity.
 
May I suggest you narrow your question down a little to something more specific. Looking at the two references you gave it seem to me to be not that hard.

I would suggest you get it working with one image (more or less cut and copy) the way you want then follow the second thread on the parts that you have to repeat for each image.

When you get your code this far and have a specific problem with it, then write back with your example code and question.

I do not use redemption so I really do not want to comment. But I think that as you are only sending an email you are really only going to need it to create your "safe email object" there are many examples of this.
 
With enough blood, sweat, and tears, and also with the generous help of Dmitry Streblechenko from dimastr.com, I was able to get a subprogram that does what I want it to. I'm going to post it here for the benefit of others.

This subprogram uses Outlook Redemption Objects (RDO) and late binding for everything because there is always the possibility of users having different versions of Outlook. I also use Option Explicit so it's necessary to create all the variables before using them.

This subprogram allows you to add attachments that are not .jpgs, while all .jpg files get embedded in the email.

To really make this subprogram work you almost need to build a separate function or subprogram that handles the gathering of attachments and puts the filenames into a string variable with each different filename separated by "|" (the pipe character). If you want to validate the email addresses or put in a default subject or message body, this separate function would be the place to do it.

If someone has a recommendation of what could be done to streamline this or make it even more useful or flexible, feel free to let me know.


Code:
Private Sub subHandleSendingEmail(sDisplayOrSend As String, _
                                sTo As String, _
                                sCC As String, _
                                sBCC As String, _
                                sSubject As String, _
                                sMsgBody As String, _
                                sAtts As String)

   
   
    'sAtts is expected to be a list of files to attach, delimited by "|" (known as a pipe)
    'I've put the delimiter in a constant called sDLM
   
   
   
    Const olFolderOutbox = 4
    Const olFolderDrafts = 16
   
   
    'This section of code will attempt to get an instance of the Outlook object using late binding.
    'If Outlook is closed the code should open Outlook.
    'If Outlook is not installed or the install is corrupted, this section of code should detect that.
    On Error Resume Next
   
    Dim oOutlookApp As Object
    Set oOutlookApp = GetObject(, "Outlook.Application")
    If Err.Number <> 0 Then
        Err.Clear
        Set oOutlookApp = CreateObject("Outlook.Application")
        If Err.Number <> 0 Then
            Audit "mdlEmail", "fHandleSendingEmail", "", "Problem creating an Outlook object. Error: " & Err.Number & " " & Err.Description
            MsgBox "Error: " & Err.Number & vbCrLf & vbCrLf & _
                    Err.Description & vbCrLf & vbCrLf & _
                    "Apparently you do not have Outlook installed or configured properly.", vbOKOnly + vbInformation, "Um..."
            Err.Clear
            Set oOutlookApp = Nothing
            Exit Sub
        End If
    End If
    On Error GoTo 0

   
    Dim oSession As Object, oMsg As Object, oAttach As Object
    Dim i As Integer, sEntryID As String
   
    On Error Resume Next
    Set oSession = CreateObject("Redemption.RDOSession")
   
    If Err.Number <> 0 Then
        MsgBox "Please contact your database administrator and give him the following message:" & vbCrLf & vbCrLf & _
            "There was a problem creating the RDOSession. Apparently Outlook Redemption Objects is not installed.", vbOKOnly + vbInformation, "Um..."
        Err.Clear
        Set oSession = Nothing
        Set oOutlookApp = Nothing
        Exit Sub
    End If
   
    On Error GoTo 0
   
    oSession.Logon
    Set oMsg = oSession.GetDefaultFolder(olFolderDrafts).Items.Add
   
    sEntryID = oMsg.EntryID

    'Multiple email addresses can be passed into the email address fields
    'by passing them into this function, separated by a semicolon
   
    'If you want to validate the email addresses to make sure they actually have an
    '@ symbol in them and have a domain name that's formatted correctly, you'll
    'need to do it before passing it into this function or do it below.
   

    oMsg.To = sTo
    oMsg.CC = sCC
    oMsg.Bcc = sBCC

    oMsg.Subject = sSubject

   
    'This code will put the attachment filenames listed in sAtts into an array
    'and then attach each file as an attachment and embed the jpegs into the body.
    If sAtts <> "" Then
        i = 0
        If InStr(sAtts, sDLM) = 0 Then sAtts = sAtts & sDLM & " "
        'Remove any doubled up delimiters
        sAtts = Replace(sAtts, sDLM & sDLM, sDLM)
        Dim aryAtt() As String
        aryAtt = Split(sAtts, sDLM)
           
        Do Until i = (UBound(aryAtt) + 1)
            'Check to see if the filename is blank before attaching it
            If Trim(aryAtt(i)) <> "" Then
                'Check to see if the file actually exists before attaching it
                If Dir(aryAtt(i)) <> "" Then
                    Set oAttach = oMsg.Attachments.Add(aryAtt(i))
                    'If the attachment is a .jpg assume that we want to embed it in the email
                    If right(aryAtt(i), 4) = ".jpg" Then
                        oAttach.Fields("MimeTag") = "image/jpeg"
                        oAttach.Fields(&H3712001E) = "picture" & CStr(i)
                        'I'm assuming we want the pictures below the optional text that's passed into this function
                        sMsgBody = sMsgBody & "<br><br><IMG align=baseline border=0 hspace=0 src=cid:picture" & CStr(i) & "><br>"
                    End If
                End If
            End If
           
            i = i + 1
        Loop
    End If
   
   
   
    oMsg.HTMLBody = sMsgBody
    oMsg.Save
   
    If LCase(sDisplayOrSend) = "send" Then
        oMsg.Send
    End If
   
    oSession.Logoff
    Set oAttach = Nothing
    Set oMsg = Nothing
    Set oSession = Nothing
   
   
    If LCase(sDisplayOrSend) = "display" Then
        Set oMsg = oOutlookApp.GetNamespace("MAPI").GetItemFromID(sEntryID)
        oMsg.Display
        Set oMsg = Nothing
    End If

    Set oOutlookApp = Nothing
   
End Sub
 
Oh man! I’m a beginner on Macro and searched a lot of websites for this issue. So glad that I found you! I registered because of this post. Lol ♥️
 

Users who are viewing this thread

Back
Top Bottom