Consulting

Results 1 to 11 of 11

Thread: Help! How do I attach an image with VBA

  1. #1
    VBAX Regular
    Joined
    Dec 2014
    Posts
    7
    Location

    Post Help! How do I attach an image with VBA

    I developed VBA code that allows me to successfully embed images into emails that I send out using an Excel spreadsheet. Since the file names of these pics differ, I would like to pass these file names to oEmail.HTMLBody = "<IMG src='cid: using an argument (i.e., mail_pic) rather than hard code in a file name like ""Scott300X276.jpg" (see code below). Can someone please tell me the proper format to use in the <IMG scr='cid section of the code?

    Thank you!


    Sub SendAttachment(what_address As String, subject_line As String, mail_image As String, mail_pic As String)
    
    
    Dim oApp As Outlook.Application
    Dim oEmail As MailItem
    Dim colAttach As Outlook.Attachments
    Dim oAttach As Outlook.attachment
     
    Set oApp = CreateObject("Outlook.Application")
    Set oEmail = oApp.CreateItem(olMailItem)
    Set colAttach = oEmail.Attachments
    Set oAttach = colAttach.Add(mail_image)
    
    '****** PROBLEM WITH THIS PART OF CODE - Don't know the proper format to pass the "mail_pic" argument above to replace
    '       the oEmail.HTMLBody below to replace the filename "Scott300X276.jpg."
    
    
    oEmail.Close olSave
    oEmail.HTMLBody = "<IMG src='cid:Scott300X276_Sidney.jpg'>"
    
    
    'oEmail.HTMLBody = "<IMG src='cid:(mail_pic)'>" <-- This does not work!
    
    '******************************************
    
    oEmail.To = what_address
    oEmail.Subject = subject_line
    
    oEmail.Send
    
    Set oEmail = Nothing
    Set colAttach = Nothing
    Set oAttach = Nothing
    Set oApp = Nothing
    End Sub
    Last edited by SamT; 12-22-2014 at 02:46 PM.

  2. #2
    On the face of it and without being able to test with your data, I would have said that the syntax would need to be

    oEmail.HTMLBody = "<IMG src='cid:" & mail_pic & "'>"
    Personally I would do it differently, editing the message directly and use late binding to Outlook e.g.

    Sub SendAttachment(what_address As String, subject_line As String, mail_image As String, mail_pic As String)
    
    Dim oApp As Object
    Dim oEmail As Object
    Dim olInsp As Object
    Dim wdDoc As Object
    Dim oRng As Object
    Dim colAttach As Object
    Dim oAttach As Object
    
        Set oApp = CreateObject("Outlook.Application")
        Set oEmail = oApp.CreateItem(0)
        Set colAttach = oEmail.Attachments
        Set oAttach = colAttach.Add(mail_image)
        With oEmail
            .BodyFormat = 2
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range(0, 0)
            oRng.InlineShapes.AddPicture Filename:=mail_pic
            .To = what_address
            .Subject = subject_line
            .Display
            .Send
        End With
        Set oEmail = Nothing
        Set colAttach = Nothing
        Set oAttach = Nothing
        Set oApp = Nothing
    End Sub
    The only disadvantage is that the message must be displayed briefly which can be disconcerting, and the flicker when merging can cause problems for those who are troubled with flashing lights.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Dec 2014
    Posts
    7
    Location
    The fix you proposed worked like a charm. Thank you VERY much!! Wow, it works so well now.
    Best regards!
    Scott

  4. #4
    VBAX Regular
    Joined
    Dec 2014
    Posts
    7
    Location

    Post Help - how do I embed a video into an Outlook e-mail?

    Now that I can successfully embed an image into an Outlook e-mail using the code below, I would like to extend this a bit by being able to embed a video. Does anyone know how to modify the following code to do this?

    ************************************
    Sub SendAttachment(what_address As String, subject_line As String, mail_image As String, mail_pic As String)


    Dim oApp As Outlook.Application
    Dim oEmail As MailItem
    Dim colAttach As Outlook.Attachments
    Dim oAttach As Outlook.attachment

    Set oApp = CreateObject("Outlook.Application")
    Set oEmail = oApp.CreateItem(olMailItem)
    Set colAttach = oEmail.Attachments
    Set oAttach = colAttach.Add(mail_image)


    oEmail.HTMLBody = "<IMG src='cid:" & mail_pic & "'>"
    oEmail.To = what_address
    oEmail.Subject = subject_line
    oEmail.Send


    Set oEmail = Nothing
    Set colAttach = Nothing
    Set oAttach = Nothing
    Set oApp = Nothing
    End Sub

    *********************************

    Thank you - Scott

  5. #5
    I would urge you strongly not to embed video files in e-mail messages. Videos typically feature huge file sizes that are not appropriate to e-mail, and even if Outlook will allow such a file, will tend to irritate recipients while their e-mail servers download the files - especially an issue if they view e-mail on a mobile telephone.

    If you want to include video, then put the video somewhere the recipients can access it e.g. dropbox or onedrive and send them a link to the file. The previous code I posted can easily be modified to do that e.g.

    Sub SendAttachment(what_address As String, subject_line As String, mail_image As String, video_link As String)
    
    Dim oApp As Object
    Dim oEmail As Object
    Dim olInsp As Object
    Dim wdDoc As Object
    Dim oRng As Object
    Dim colAttach As Object
    Dim oAttach As Object
    Dim strLinkText As String
        strLinkText = "Click here for video."
    
        Set oApp = CreateObject("Outlook.Application")
        Set oEmail = oApp.CreateItem(0)
        Set colAttach = oEmail.Attachments
        Set oAttach = colAttach.Add(mail_image)
        With oEmail
            .BodyFormat = 2
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range(0, 0)
            oRng.Text = "This is the body text of the message" & vbCr & vbCr & _
            "The link appears below this text, and before the default signature." & vbCr & vbCr
            oRng.collapse 0
            wdDoc.Hyperlinks.Add Anchor:=oRng, _
                                 Address:=video_link, _
                                 SubAddress:="", _
                                 ScreenTip:="", _
                                 TextToDisplay:=strLinkText
            .To = what_address
            .Subject = subject_line
            .Display
            '.Send 'resurrect this line after testing.
        End With
        Set oEmail = Nothing
        Set colAttach = Nothing
        Set oAttach = Nothing
        Set oApp = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Regular
    Joined
    Dec 2014
    Posts
    7
    Location
    Thank you very much for the advise and also the code. It is most appreciated!
    I hope you have a very nice new year.
    Best regards,Scott

  7. #7
    VBAX Regular
    Joined
    Dec 2014
    Posts
    7
    Location
    When I use the code below (as recommended above), I get a Run-time error 91 (Object variable or with block variable not set) error. Any idea where the problem is?
    Thank you!


    Sub SendVideo(what_address As String, subject_line As String, cover As String, video_link As String)


    Dim oApp As Object
    Dim oEmail As Object
    Dim olInsp As Object
    Dim wdDoc As Object
    Dim oRng As Object
    Dim colAttach As Object
    Dim oAttach As Object
    Dim strLinkText As String
    strLinkText = "Click here for video."


    Set oApp = CreateObject("Outlook.Application")
    Set oEmail = oApp.CreateItem(0)
    Set colAttach = oEmail.Attachments
    Set oAttach = colAttach.Add(cover)
    With oEmail
    .BodyFormat = 2
    Set olInsp = .GetInspector
    Set wdDoc = olInsp.WordEditor
    Set oRng = wdDoc.Range(0, 0) '<------- ERROR AT THIS LINE
    oRng.Text = "This is the body text of the message" & vbCr & vbCr & _
    "The link appears below this text, and before the default signature." & vbCr & vbCr
    oRng.collapse 0
    wdDoc.Hyperlinks.Add Anchor:=oRng, _
    Address:=video_link, _
    SubAddress:="", _
    ScreenTip:="", _
    TextToDisplay:=strLinkText
    .To = what_address
    .Subject = subject_line
    .Display
    .Send 'resurrect this line after testing.
    End With
    Set oEmail = Nothing
    Set colAttach = Nothing
    Set oAttach = Nothing
    Set oApp = Nothing


    End Sub

  8. #8
    VBAX Regular
    Joined
    Dec 2014
    Posts
    7
    Location

    Post

    When I use the code below (as recommended above), I get a Run-time error 91 (Object variable or with block variable not set) error. Any idea where the problem is?
    Thank you!


    Sub SendVideo(what_address As String, subject_line As String, cover As String, video_link As String)


    Dim oApp As Object
    Dim oEmail As Object
    Dim olInsp As Object
    Dim wdDoc As Object
    Dim oRng As Object
    Dim colAttach As Object
    Dim oAttach As Object
    Dim strLinkText As String
    strLinkText = "Click here for video."


    Set oApp = CreateObject("Outlook.Application")
    Set oEmail = oApp.CreateItem(0)
    Set colAttach = oEmail.Attachments
    Set oAttach = colAttach.Add(cover)
    With oEmail
    .BodyFormat = 2
    Set olInsp = .GetInspector
    Set wdDoc = olInsp.WordEditor
    Set oRng = wdDoc.Range(0, 0) '<------- ERROR AT THIS LINE
    oRng.Text = "This is the body text of the message" & vbCr & vbCr & _
    "The link appears below this text, and before the default signature." & vbCr & vbCr
    oRng.collapse 0
    wdDoc.Hyperlinks.Add Anchor:=oRng, _
    Address:=video_link, _
    SubAddress:="", _
    ScreenTip:="", _
    TextToDisplay:=strLinkText
    .To = what_address
    .Subject = subject_line
    .Display
    .Send 'resurrect this line after testing.
    End With
    Set oEmail = Nothing
    Set colAttach = Nothing
    Set oAttach = Nothing
    Set oApp = Nothing


    End Sub

  9. #9
    It works for me, but change the errant line to

    Set oRng = wdDoc.Range
    oRng.collapse 1
    and see if you fare any better.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  10. #10
    VBAX Regular
    Joined
    Dec 2014
    Posts
    7
    Location

    Post Still having run-time error issue

    No, still getting the same run-time error. I am wondering if it might be VBA Project references. Any idea which references must be checked or any other possible solutions?
    Thank you!
    Scott

  11. #11
    On the 24th December you reported that 'The fix you proposed worked like a charm. Thank you VERY much!! Wow, it works so well now.' The original macro used exactly the same method of accessing the message body range, so the implication is that something else is afoot. Reboot the PC. When it has restarted, before running Office, see http://www.gmayor.com/what_to_do_when_word_crashes.htm and address any issue of orphaned temporary files. Then try again.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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