3

My goal:

  • display one mail to the user for editing
  • create another mail with the same body content

My problem:

I can copy everything from the first mail except the image from my signature. Why is that? This code is (and needs to be) run in excel. Here is a minimal example of my problem for you to test, run code from excel and add a reference to Microsoft Outlook Object Library; go through code step by step with "F8" to see details.

Option Explicit
 Dim objOutlook As Outlook.Application
 Dim objOutlookMsgTemplate As Outlook.MailItem
 Dim objOutlookMsg2 As Outlook.MailItem

Sub main()
 CreateOutlookSession
 CopyMailBody
End Sub

Function CreateOutlookSession()
 On Error Resume Next
     Set objOutlook = GetObject(, "Outlook.Application")
     If Err <> 0 Then
         Set objOutlook = CreateObject("Outlook.Application")
     End If
 On Error GoTo 0
End Function

Function CopyMailBody()
 Set objOutlookMsgTemplate = objOutlook.CreateItem(olMailItem)
 Set objOutlookMsg2 = objOutlook.CreateItem(olMailItem)
 With objOutlookMsg1
     .Display 'this mail has a signature with pictures, if your default sig contains some
     .HTMLBody = "signature below" & .HTMLBody
 End With
 With objOutlookMsg2
     .HTMLBody = objOutlookMsgTemplate.HTMLBody 'this copies everything but pictures from my signature.. why?
     .Display 'this mail does not contain the image from mail 1, just an area of the same size with a "file not found" message within
 End With
End Function

I am able to copy everything (including sig. pictures) with the method mentioned in this SO question so I have a workaround, but I wonder why a simple copy/paste of the HTML body is not working, since i merely copy HTML text.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Sorry, scratch last, didn't read the question properly. Just quick things to note; first, I'm not sure if that link is to the correct post - I don't see what sharing macros has to do with this question! Second, I'm not sure whether your code works - I can't test it as I don't have outlook - but I think your first function should be `CreateOutlookSession() As Outlook.Application` and your second Function should be a `Sub CopyMailBody(objOutlook As Outlook.Application)` where its argument is the result of the first function, but is should not be a function itself as it doesn't return anything. – Greedo Jun 11 '17 at 12:03
  • Thanks for input. Link was indeed wrong, fixed that. Added the needed `Dim`s and formatted code to a single bunch for copy/paste. –  Jun 11 '17 at 12:49
  • 1
    You should post your solution as an answer to the question and mark it as the accepted answer instead of editing the question. – Lews Therin Jun 20 '17 at 14:03
  • Ah, I did not know that I can accept my own answers. Thank you for the hint, will do. –  Jun 20 '17 at 14:06

1 Answers1

1

Solution:

The signature picture is attached to the message and hidden (as in not seen as an attached picture when .Display is used). In order to copy the full signature with images, I just need to copy all attachments as well.