如何计算Outlook中的附件总数

时间:2018-06-28 13:45:58

标签: vba outlook outlook-vba

我实际上正在检查以下代码,以对选定电子邮件中的附件进行计数。

    Sub CountAttachmentsinSelectedEmails()
    Dim olSel As Selection
    Dim oMail As Object
    Dim AttCount As Long
    Dim strMsg As String

    Set olSel = Outlook.Application.ActiveExplorer.Selection

    For Each oMail In olSel
        'To confirm if the selected items are all emails
        If oMail.Class <> olMail Then
           strMsg = "Please select mail items only!"
           nRes = MsgBox(strMsg, vbOKOnly + vbExclamation)
           Exit Sub
        End If
        'Get the total number of the attachments in selected emails
        AttCount = oMail.Attachments.Count + AttCount
    Next

    strMsg = "There are " & AttCount & " attachments in the " & olSel.Count & " selected emails."
    nRes = MsgBox(strMsg, vbOKOnly + vbInformation, "Count Attachments")
End Sub

但这实际上是在考虑签名中的徽标以及电子邮件正文中的任何嵌入或插入的图片,并显示错误的结果。

因此,在这里我需要以下两个问题的帮助:

  1. 有什么办法可以跳过它们?

  2. 是否有任何代码可以计算电子邮件中zip或rar文件附件中的文档总数?

如果有任何代码,我们可以在这里包括吗?

2 个答案:

答案 0 :(得分:0)

未经测试,但一种方法是在所有附件上循环并检查其文件名是否以.zip.rar结束

  Option Explicit

  Sub CountAttachmentsinSelectedEmails()
    Dim olSel As Selection
    Dim oMail As Outlook.MailItem
    Dim AttCount As Long
    Dim strMsg As String
    Dim nRes As Long
    Dim objAttach As Outlook.Attachment

    Set olSel = Outlook.Application.ActiveExplorer.Selection

    For Each oMail In olSel
        'To confirm if the selected items are all emails
        If oMail.Class <> olMail Then
           strMsg = "Please select mail items only!"
           nRes = MsgBox(strMsg, vbOKOnly + vbExclamation)
           Exit Sub
        End If

        'Loop on attachements
        For Each objAttach In Item.Attachments
            'increment counter if the attachement extention is .zip or .rar
            If LCase(Right(objAttach.FileName, 4)) = ".rar" Or LCase(Right(objAttach.FileName, 4)) = ".zip" Then
                AttCount = AttCount + 1
            End If
        Next objAttach 

    Next

    strMsg = "There are " & AttCount & " attachments in the " & olSel.Count & " selected emails."
    nRes = MsgBox(strMsg, vbOKOnly + vbInformation, "Count Attachments")
End Sub

答案 1 :(得分:0)

您想要做的两件事都有些棘手。

我不知道是否有一种可预测的方法来确定给定的附件是徽标还是嵌入式图像。可能会有,但是一些快速测试表明,Outlook是将附件的AttachmentType报告为olByValue1),无论它是签名,徽标,PDF还是其他形式。如果您确定所有徽标附件都具有相似的名称(例如,在您的数量中,请跳过名为image001.jpg的文件),则可能会对“黑名单”特定的文件名或附件感到很幸运。将特定附件列入白名单,并且仅显示例如Excel,Word或PDF文件的附件。

关于ZIP / RAR档案:VBA似乎没有本机支持来打开ZIP档案。但是,看来您可以调用Shell进行处理。您可能要开始搜索类似this的东西。