使用 Outlook VBA 区分可见和不可见的附件 [英] Distinguish visible and invisible attachments with Outlook VBA

查看:22
本文介绍了使用 Outlook VBA 区分可见和不可见的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求是将附件上传到服务器.但是,我们只想上传那些出现在 Outlook 的消息"行(见下图)中的内容,而不是电子邮件正文中的其他内容.

由于 Outlook 本身知道该行中应显示哪个附件,因此必须有用于在内部区分它们的信息.

那么,我怎样才能在我的 VBA 程序中做到这一点?我尝试使用 MailItem.Attachments 但所有附件都在那里,我找不到它们的任何属性可以用来区分.

更新原标题用 Outlook VBA 区分嵌入的附件"有点误导.所以我改了标题.

展望截图:

解决方案

正如我目前所测试的那样,嵌入的附件总是有一个 MIME 内容 ID,无论它是否出现在邮件正文中.所以解决办法是检查它是否有一个内容ID.

这是一个计算可见附件的示例代码:

Sub ShowVisibleAttachmentCount()Const PR_ATTACH_CONTENT_ID As String = "http://schemas.microsoft.com/mapi/proptag/0x3712001F"Const PR_ATTACHMENT_HIDDEN As String = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"昏暗为邮件项目将附件变暗Dim pa 作为 PropertyAccessorDim c 作为整数Dim cid 作为字符串昏暗的身体作为字符串c = 0设置 m = Application.ActiveInspector.CurrentItembody = m.HTMLBodyFor each a In m.Attachments设置 pa = a.PropertyAccessorcid = pa.GetProperty(PR_ATTACH_CONTENT_ID)如果 Len(cid) >0 那么如果 InStr(body, cid) 那么别的'如果 PR_ATTACHMENT_HIDDEN 不存在,'会发生错误.我们只是忽略这个错误并且'把它当作假的.出错时继续下一步如果不是 pa.GetProperty(PR_ATTACHMENT_HIDDEN) 然后c = c + 1万一出错时转到 0万一别的c = c + 1万一下一个消息框结束子

当我浏览所有 Outlook 电子邮件时,它会在行中提供相同数量的附件.

更新

感谢 Dmitry Streblechenko 的信息,我使用 Java 电子邮件库生成的电子邮件测试了 Outlook.结果显示,当电子邮件附件包含 CID 但未出现在电子邮件正文中时,它会出现在附件行中.

更新

看来还是有些情况,这还不够好.我生成了以下 MIME 电子邮件正文:

Message-ID: <1044564324.2.1360638429705.JavaMail.joe@xxxx>主题:测试MIME 版本:1.0内容类型:多部分/替代;边界="----=_Part_0_1327112367.1360638429515"返回路径:xxxx@xxxx.xxxX-OriginalArrivalTime:2013 年 2 月 12 日 03:07:16.0096 (UTC) FILETIME=[0FC1B000:01CE08CE]-------=_Part_0_1327112367.1360638429515内容类型:文本/纯文本;字符集=us-ascii内容传输编码:7 位电报-------=_Part_0_1327112367.1360638429515内容类型:多部分/相关;边界="----=_Part_1_1747887937.1360638429520"------=_Part_1_1747887937.1360638429520内容类型:文本/html;字符集=UTF-8内容传输编码:引用打印<html><head><meta http-equiv=3D'content-type' content=3D'text/html;字符集==3DUTF-8'></head><title>TTT</title><body><img src=3D"cid:test1.png" alt=3D'==E6=81=AD=E8=B4=BA=E6=96=B0=E7=A6=A7'/><p>txt</p></body></html>------=_Part_1_1747887937.1360638429520内容类型:图像/png内容传输编码:base64内容 ID:<test.png>iVBORw0KGgoAAAANSUhEUgAAAIIAAAAmCAYAAAAIjkMFAAABHUlEQVR42u3Z0Q7CIAyFYd//pafxckFoS9ELvz8aE9mQrIfTFh8PAAAAAPgp1+t1vT9i32fm6FzP6JrKb3aulRAGARm9Z9dUAhWZY7Wm7Hr+IvhdD+s+PhLCLNBZQZ12HI7QlBqyQohctxM8bvAFIcx2eEYIo/vuY5WAi3BzWlhZ+if7zs7TUWtE10Asgd3bUSxWHvrMobJOtXITQkjk5Z3gdaWaqBBWouYIhdy+E+TsPNHU0CUEbjDJ49GxE0KInBNUheAcYbPVy9QNmRaxUvVHd7Idf0gU2QDOduVqnkinoEb4QY1Q3V2RNrMqpB0h6BqKh0gZIWT/AzjVycwcjSMcPI3buSebZiptaLbIBQAAAAAAAAAAAAAAP6OJyO5jJ4bZa/gAAAAAAElFTkSuQmCC------=_Part_1_1747887937.1360638429520--------=_Part_0_1327112367.1360638429515--

请注意,我更改了正文中的引用图像内容 ID,而实际图像的内容 ID 错误(因此未引用).然而,图像不在电子邮件的主要部分(它在替代部分的一个分支中).这使它在 Outlook 中不可见.

所以为了检测我们必须确保附件出现在主要的 MIME 部分......正在寻找这样做的方法.

更新

进一步挖掘我到达了这个链接,我添加了另一个测试 - PR_ATTACHMENT_HIDDEN 属性.

另外值得一提的是,2010 年展望本身并不一致.我观察到有时电子邮件列表会显示附件图标以指示附件的存在,但在检查器中打开它时却没有任何显示.

参考文献:

使用 VBS 发送带有嵌入图像的 Outlook 电子邮件

MSDN - 附件属性

论坛 - 识别内嵌附件

The requirement is to upload attachments to a server. However, we only want to upload those appears in the "Message" line (see pic below) of outlook, nothing else in the email body.

Since Outlook itself knows which attachment should be show in the line, there must be information that it uses to distinguish them internally.

So, how can I do that within my VBA program? I have tried to use MailItem.Attachments but all attachments are there and I cannot find any property of them can be used to distinguish.

UPDATE The original title "Distinguish embadded attachments with Outlook VBA" is a bit misleading. So I have changed the title.

Outlook Screenshot:

解决方案

As I can test so far, an embedded attachment always have a MIME content ID, regardless whether it appears in the mail body. So the solution is to check whether it has a content ID.

Here is an example code that counts the visible attachments:

Sub ShowVisibleAttachmentCount()
    Const PR_ATTACH_CONTENT_ID As String = "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
    Const PR_ATTACHMENT_HIDDEN As String = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"

    Dim m As MailItem
    Dim a As Attachment
    Dim pa As PropertyAccessor
    Dim c As Integer
    Dim cid as String

    Dim body As String

    c = 0

    Set m = Application.ActiveInspector.CurrentItem
    body = m.HTMLBody

    For Each a In m.Attachments
        Set pa = a.PropertyAccessor
        cid = pa.GetProperty(PR_ATTACH_CONTENT_ID)

        If Len(cid) > 0 Then
            If InStr(body, cid) Then
            Else
                'In case that PR_ATTACHMENT_HIDDEN does not exists, 
                'an error will occur. We simply ignore this error and
                'treat it as false.
                On Error Resume Next
                If Not pa.GetProperty(PR_ATTACHMENT_HIDDEN) Then
                    c = c + 1
                End If
                On Error GoTo 0
            End If
        Else
            c = c + 1
        End If
    Next a
    MsgBox c
End Sub

When I run through all my outlook emails it gives the same number of attachments in the line.

UPDATE

Thanks to Dmitry Streblechenko's information, I tested Outlook with email generated by Java Email library. The result shows that when an email attachment contains an CID but not appear in the email body, it will appear in the attachments line.

UPDATE

It looks there are still some cases that this is not good enough. I have generated the following MIME email body:

Message-ID: <1044564324.2.1360638429705.JavaMail.joe@xxxx>
Subject: Test
MIME-Version: 1.0
Content-Type: multipart/alternative; 
    boundary="----=_Part_0_1327112367.1360638429515"
Return-Path: xxxx@xxxx.xxx
X-OriginalArrivalTime: 12 Feb 2013 03:07:16.0096 (UTC) FILETIME=[0FC1B000:01CE08CE]

------=_Part_0_1327112367.1360638429515
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

TTT
------=_Part_0_1327112367.1360638429515
Content-Type: multipart/related; 
    boundary="----=_Part_1_1747887937.1360638429520"

------=_Part_1_1747887937.1360638429520
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html><head><meta http-equiv=3D'content-type' content=3D'text/html; charset=
=3DUTF-8'></head><title>TTT</title><body><img src=3D"cid:test1.png" alt=3D'=
=E6=81=AD=E8=B4=BA=E6=96=B0=E7=A6=A7' /><p>txt</p></body></html>
------=_Part_1_1747887937.1360638429520
Content-Type: image/png
Content-Transfer-Encoding: base64
Content-ID: <test.png>

iVBORw0KGgoAAAANSUhEUgAAAIIAAAAmCAYAAAAIjkMFAAABHUlEQVR42u3Z0Q7CIAyFYd//pafx
ckFoS9ELvz8aE9mQrIfTFh8PAAAAAPgp1+t1vT9i32fm6FzP6JrKb3aulRAGARm9Z9dUAhWZY7Wm
7Hr+IvhdD+s+PhLCLNBZQZ12HI7QlBqyQohctxM8bvAFIcx2eEYIo/vuY5WAi3BzWlhZ+if7zs7T
UWtE10Asgd3bUSxWHvrMobJOtXITQkjk5Z3gdaWaqBBWouYIhdy+E+TsPNHU0CUEbjDJ49GxE0KI
nBNUheAcYbPVy9QNmRaxUvVHd7Idf0gU2QDOduVqnkinoEb4QY1Q3V2RNrMqpB0h6BqKh0gZIWT/
AzjVycwcjSMcPI3buSebZiptaLbIBQAAAAAAAAAAAAAAAP6OJyO5jJ4bZa/gAAAAAElFTkSuQmCC

------=_Part_1_1747887937.1360638429520--

------=_Part_0_1327112367.1360638429515--

Notice that I have changed the referencing image content id in the body, and the actual image have a wrong content ID (so it is not referenced). However the image is not in the main part of the email (it is in a branch of an alternative part). That makes it invisible in outlook.

So to detect we have to make sure the attachment appears in the main MIME part... Looking for ways to do so.

UPDATE

Further digging I reaches this link and I added one more test - the PR_ATTACHMENT_HIDDEN property.

Also it is worth to say that outlook 2010 itself is not consistent. I have observed that sometimes the email list shows the attachment icon to indicate existence of attachments but there are nothing appear when opening it in an inspector.

References:

Sending Outlook Email with embedded image using VBS

MSDN - Attachment Properties

Forum - Identifying inline attachments

这篇关于使用 Outlook VBA 区分可见和不可见的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆