Consulting

Results 1 to 7 of 7

Thread: Missing Image while replying through VBA

  1. #1
    VBAX Newbie
    Joined
    Aug 2018
    Posts
    4
    Location

    Missing Image while replying through VBA

    HI Expert,

    I am almost blocked in my project for now. Issue I am facing is when I am posting a reply through outlook VBA, all is OK except images in the mail, it just shows missing image.

    Below is my code. Without image being included in reply, I can not proceed

    Please advice me on this, how to proceed

    Sub send_reply(ByVal Item As Outlook.MailItem)
       Dim olReply As MailItem
       Item.Actions("Reply").ReplyStyle = olIncludeOriginalText
       If Item.Class = olMail Then
           Set olReply = Item.Reply
        Else
           Exit Sub
        End If
        
       olReply.BodyFormat = olFormatHTML
        
       olReply.subject = Item.subject
       olReply.To = Item.Sender
       
        With olReply
             .HTMLBody = "This is a test auto reply." & vbCrLf & Item.HTMLBody
             
             .Send
        End With
       
       
       
    End Sub

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    606
    Location
    .
    .HTMLBody = eBody & "<br></br>" & "<img src=""C:\Users\logit\Documents\Logo.bmp"">"
    Note that you need to include the path to the image that is inserted into your html body.

    The above code references a few lines of text, previously identiified as ..... eBody = "This is the body of text to be included with the email message."
    Then the html symbols for adding a blank line are ..... <br> </br> ..... and finally the html code for inserting the image.

  3. #3
    VBAX Newbie
    Joined
    Aug 2018
    Posts
    4
    Location
    Quote Originally Posted by Logit View Post
    .
    .HTMLBody = eBody & "<br></br>" & "<img src=""C:\Users\logit\Documents\Logo.bmp"">"
    Note that you need to include the path to the image that is inserted into your html body.

    The above code references a few lines of text, previously identiified as ..... eBody = "This is the body of text to be included with the email message."
    Then the html symbols for adding a blank line are ..... <br> </br> ..... and finally the html code for inserting the image.
    Thanks Logit for quick help.

    However, I have some doubts here. My VBA script will be processing mails received and incoming mail may have images in it, so I am not really sure that what will be image path. Along with it, mail content may be a long mail thread where images and text are spread in order of people replying. Appending all images at the end will lose the order.

    Can you please provide any hint to me, what should be the way here?

  4. #4
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    606
    Location
    .
    The path included in the code tidbit refers to a location on my computer. You will need to change it to match your computer where you will store the image/s.

    From the remainder of your description it appears you are wanting to capture the incoming images and then your response would include those images ( in the order of the emails received )
    and any image/s you may add in your reply.

    You are going to need more code to do all that. Something that will capture / store / copy then paste the body of the incoming email to your outgoing email first, then add your reply at the bottom
    with your own image/s.

    So, as far as adding your images to outgoing email, the line of code would still be the same. Your outgoing email will still need to know the path where the image/s you are adding are located.

    The following are untested here but provided for your review and hopefully success :

    https://stackoverflow.com/questions/...-vba-or-macros

    https://www.ozgrid.com/forum/forum/h...el-and-save-it

    For the above two resources my search term was : " vba copy body & images incoming email "

  5. #5
    Reading between the lines here, the image(s) you are talking about refer to images in the original message bodies. That being the case, you need to use the message body inspector e.g. in order to edit the body of the reply message and add your text (and default signature) to that message leaving the rest in place. If you want to add images from file to your message body then set the range (orng) appropriately and insert the image into the range, much as you would in a Word document (which is essentially what the message body is).

    I have added a macro so that you can test it.

    Option Explicit
    
    Sub send_reply(ByVal Item As Outlook.MailItem)
    Dim olReply As MailItem
    Dim olInsp As Inspector
    Dim wdDoc As Object
    Dim oRng As Object
        Item.Actions("Reply").ReplyStyle = olIncludeOriginalText
        If TypeName(Item) = "MailItem" Then
            Set olReply = Item.Reply
        Else
            GoTo lbl_Exit
        End If
        'olReply.Subject = Item.Subject
        'olReply.To = Item.sender
        With olReply
            .BodyFormat = olFormatHTML
            .Display 'required
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range
            oRng.collapse 1
            oRng.Text = "This is a test auto reply." & vbCr
            '.Send 'Restore after testing
        End With
    lbl_Exit:
        Set olReply = Nothing
        Exit Sub
    End Sub
    
    Sub TestMacro()
    Dim olMsg As MailItem
        On Error Resume Next
        Set olMsg = ActiveExplorer.Selection.Item(1)
        send_reply olMsg
    lbl_Exit:
        Set olMsg = Nothing
        Exit Sub
    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 Newbie
    Joined
    Aug 2018
    Posts
    4
    Location
    Quote Originally Posted by Logit View Post
    .
    The path included in the code tidbit refers to a location on my computer. You will need to change it to match your computer where you will store the image/s.

    From the remainder of your description it appears you are wanting to capture the incoming images and then your response would include those images ( in the order of the emails received )
    and any image/s you may add in your reply.

    You are going to need more code to do all that. Something that will capture / store / copy then paste the body of the incoming email to your outgoing email first, then add your reply at the bottom
    with your own image/s.

    So, as far as adding your images to outgoing email, the line of code would still be the same. Your outgoing email will still need to know the path where the image/s you are adding are located.

    The following are untested here but provided for your review and hopefully success :

    https://stackoverflow.com/questions/...-vba-or-macros

    https://www.ozgrid.com/forum/forum/h...el-and-save-it

    For the above two resources my search term was : " vba copy body & images incoming email "
    Thanks Logit for your help, I will try and update

  7. #7
    VBAX Newbie
    Joined
    Aug 2018
    Posts
    4
    Location
    Thanks Graham, I will try this option and update

Posting Permissions

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