PDA

View Full Version : Missing Image while replying through VBA



ajeetk
08-03-2018, 06:23 AM
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 :banghead:

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

Logit
08-03-2018, 08:35 AM
.


.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.

ajeetk
08-03-2018, 08:50 AM
.


.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?

Logit
08-03-2018, 11:23 AM
.
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/11876549/how-to-copy-outlook-mail-message-into-excel-using-vba-or-macros

https://www.ozgrid.com/forum/forum/help-forums/excel-general/97730-vba-code-to-open-specific-email-and-copy-the-email-body-to-excel-and-save-it

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

gmayor
08-03-2018, 11:37 PM
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

ajeetk
08-04-2018, 09:27 AM
.
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/11876549/how-to-copy-outlook-mail-message-into-excel-using-vba-or-macros

https://www.ozgrid.com/forum/forum/help-forums/excel-general/97730-vba-code-to-open-specific-email-and-copy-the-email-body-to-excel-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

ajeetk
08-04-2018, 09:27 AM
Thanks Graham, I will try this option and update