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

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

问题描述

要求是将附件上传到服务器。但是,我们只希望上载显示在Outlook的消息行(请参见下图)中的内容,而不上载电子邮件正文中的其他内容。

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.

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

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

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

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.

更新
原始标题使用Outlook VBA区分嵌入的附件有点误导。因此,我更改了标题。

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

Outlook屏幕快照:

Outlook Screenshot:

推荐答案

到目前为止,我可以测试的是,嵌入式附件始终具有MIME内容ID,无论它是否出现在邮件正文中。因此,解决方案是检查它是否具有内容ID。

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

当我浏览所有Outlook电子邮件时,该行中的附件数量相同。

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

更新

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

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.

更新

在某些情况下,这似乎还不够好。
我生成了以下MIME电子邮件正文:

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

请注意,我已经更改了正文中的引用图像内容ID,而实际图像的内容ID错误(因此未引用)。但是,图像不在电子邮件的主要部分中(它在替代部分的分支中)。

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.

因此,要进行检测,我们必须确保附件出现在MIME的主要部分中……正在寻找实现方法。

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

更新

进一步挖掘,我到达此链接,我又添加了一个测试- PR_ATTACHMENT_HIDDEN 属性。

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

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

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.

参考文献:

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

MSDN-附件属性

论坛-识别嵌入式附件

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

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