Outlook 2010 电子邮件到网页,包括图像
Outlook 2010 email to Web page, including images
我可以使用以下代码将 Outlook 电子邮件粘贴到网页中。
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());
});
});
效果很好除非HTML正文中有图像。在那种情况下,我得到一个损坏的图像 src,如下所示:
<img width=596 height=381
id="Picture_x0020_1"
src="cid:image001.png@01D07855.C2524830"
>
有没有办法在 VBA 函数中对图像数据进行编码,最好是作为数据 URI?
src="cid:image001.png@01D07855.C2524830"
此类源字符串表示带有 PR_ATTACH_CONTENT_ID(DASL 名称 - "http://schemas.microsoft.com/mapi/proptag/0x3712001E") property set to the image001.png@01D07855.C2524830 value. You can find the image using the Attachments 属性 Outlook 项目的隐藏附件。
有关详细信息,请参阅 How to add an embedded image to an HTML message in Outlook 2010。
想出了一个解决方案,感谢 Eugene 将我指向附件集合。 (我不知道它包含嵌入式图像。)
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
此代码:
- 遍历附件集合以查找嵌入的图像。
- 将图像保存到临时文件。
- 使用此处的
EncodeFile
函数将图像二进制数据转换为 base64:
- 将每个图像的
src
属性替换为 base64 编码,将其转换为数据 URI。
我会对避免创建临时文件的方法感兴趣。
我现在可以 运行 Outlook 中的宏并粘贴到我的网页上,图像数据嵌入 HTML 本身。
我可以使用以下代码将 Outlook 电子邮件粘贴到网页中。
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());
});
});
效果很好除非HTML正文中有图像。在那种情况下,我得到一个损坏的图像 src,如下所示:
<img width=596 height=381
id="Picture_x0020_1"
src="cid:image001.png@01D07855.C2524830"
>
有没有办法在 VBA 函数中对图像数据进行编码,最好是作为数据 URI?
src="cid:image001.png@01D07855.C2524830"
此类源字符串表示带有 PR_ATTACH_CONTENT_ID(DASL 名称 - "http://schemas.microsoft.com/mapi/proptag/0x3712001E") property set to the image001.png@01D07855.C2524830 value. You can find the image using the Attachments 属性 Outlook 项目的隐藏附件。
有关详细信息,请参阅 How to add an embedded image to an HTML message in Outlook 2010。
想出了一个解决方案,感谢 Eugene 将我指向附件集合。 (我不知道它包含嵌入式图像。)
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
此代码:
- 遍历附件集合以查找嵌入的图像。
- 将图像保存到临时文件。
- 使用此处的
EncodeFile
函数将图像二进制数据转换为 base64: - 将每个图像的
src
属性替换为 base64 编码,将其转换为数据 URI。
我会对避免创建临时文件的方法感兴趣。
我现在可以 运行 Outlook 中的宏并粘贴到我的网页上,图像数据嵌入 HTML 本身。