0

I can paste an Outlook email message into a Web page using the following code.

VBA

Sub HTMLClipboard()
  Dim M As MailItem, Buf As MSForms.DataObject
  Set M = ActiveExplorer().Selection.Item(1)
  Set Buf = New MSForms.DataObject
  Buf.SetText M.HTMLBody
  Buf.PutInClipboard
End Sub

HTML

<div id="Data"></div>
<textarea id="TA"></textarea>

jQuery

$(document).on('paste', function(e) {
  $('#TA').focus();
  setTimeout(function() {
    $('#Data')
      .html($('#TA').val());
  });
});

This works great unless there's an image in the HTMLBody. In that situation, I get a broken image src like this:

<img width=596 height=381
 id="Picture_x0020_1" 
 src="cid:[email protected]"
>

Is there a way to encode image data within the VBA function, preferably as a Data URI?

Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79

2 Answers2

1

src="cid:[email protected]"

Such source string indicates a hidden attachment with the PR_ATTACH_CONTENT_ID (DASL name - "http://schemas.microsoft.com/mapi/proptag/0x3712001E") property set to the [email protected] value. You can find the image using the Attachments property of Outlook items.

See How to add an embedded image to an HTML message in Outlook 2010 for more information.

Community
  • 1
  • 1
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

Came up with a solution, and thanks to Eugene for pointing me towards the Attachments collection. (I didn't know it included embedded images.)

Sub HTMLClipboard()
  Dim M As MailItem, Buf As MSForms.DataObject
  Set M = ActiveExplorer().Selection.Item(1)
  Set Buf = New MSForms.DataObject

  b = M.HTMLBody

  Dim i As Integer
  For i = 1 To M.Attachments.Count
    fn = M.Attachments.Item(i).PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E")
    If fn > "" Then
      M.Attachments.Item(i).SaveAsFile "d:\temp"
      base64 = EncodeFile("d:\temp")
      b = Replace(b, "cid:" & fn, "data:image/png;base64," & base64)
    End If
  Next
  Buf.SetText b
  Buf.PutInClipboard
End Sub

This code:

  1. Iterates through the Attachments collection looking for embedded images.
  2. Saves the image to a temporary file.
  3. Converts the image binary data to base64 using the EncodeFile function found here: https://stackoverflow.com/a/8134022/3903374
  4. Replaces each image's src attribute with the base64 encoding, which turns it into a Data URI.

I'd be interested in a method that avoids creating a temporary file.

I can now run the macro in Outlook and paste onto my Web page, with image data being embedded in the HTML itself.

Community
  • 1
  • 1
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79