按类型过滤电子邮件附件文件

时间:2015-07-23 05:30:58

标签: vba email outlook-vba

我有一个脚本,当没有在电子邮件中找到附件时发送通知。是否可以检查附件的文件类型,如果文件类型不是所需的文件类型,则发送通知。

得到这样的代码。

    Option Explicit
        Public Sub CheckAttachment(Item As Outlook.MailItem)
            Dim olInspector As Outlook.Inspector
            Dim olDocument As Outlook.DocumentItem
            Dim olSelection As Outlook.Selection
            Dim objAtt As Outlook.Attachment
            Dim ft As FileTypes
            Dim olReply As MailItem
            Dim FileExtension As String
            FileExtension = "jpeg, jpg, tiff, pdf"

            '// Check for attachment
            If Item.Attachments.Count > 1 Then
            GoTo CheckFileType1
                End If



        CheckFileType1:
            If Item.Attachments(Item.Attachments, ".tiff") Then
            GoTo CheckFileType2
            End If

        CheckFileType2:
            If Item.Attachments(Item.Attachments, ".jpeg") Then
            GoTo CheckFileType3
            End If

        CheckFileType3:
            If Item.Attachments(Item.Attachments, ".pdf") Then
            GoTo SendMail
            Else
            Exit Sub
            End If

        SendMail:
            Set olReply = Item.Reply '// Reply if no attachment found
            olReply.Body = "No attachment was found. Re-send the email and ensure that the needed file is attached." & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & "This is a system generated message. No need to reply. Thank you."
            olReply.Send

            Set olInspector = Nothing
            Set olDocument = Nothing
            Set olSelection = Nothing


        End Sub

2 个答案:

答案 0 :(得分:1)

  

是否可以检查附件的文件类型,并在文件类型不是所需文件类型时发送通知。

是的,是的。

Attachment类提供FileName属性,该属性返回表示附件文件名的字符串。

答案 1 :(得分:1)

我会使用select case function,效果会更好

Option Explicit
Public Sub CheckAttachment(Item As Outlook.MailItem)
    Dim olInspector As Outlook.Inspector
    Dim olDocument As Outlook.DocumentItem
    Dim olSelection As Outlook.Selection
    Dim olReply As MailItem
    Dim olAtt As Attachment
    Dim olFileType As String

    '// Check for attachment
    If Item.Attachments.Count > 0 Then
        For Each olAtt In Item.Attachments
            '// The code looks last 4 characters,
            '// including period and will work as long
            '// as you use 4 characters in each extension.
            olFileType = LCase$(Right$(olAtt.FileName, 4))

            '// Select Case File type
            Select Case olFileType
                '// Add additional file types below as needed
                Case ".pdf", "docx", ".doc", ".xls", "xlsx"
            Exit Sub
                Case Else
                GoTo Reply
            End Select
        Next
    Else
Reply:
        Set olReply = Item.Reply '// Reply if no attachment found
        olReply.Body = "No attachment was found. Re-send Attchment " 
        olReply.Send

    End If

    Set olInspector = Nothing
    Set olDocument = Nothing
    Set olSelection = Nothing
    Set olAtt = Nothing
End Sub

修改评论

多行尝试

olReply.Body = "Dear sender," & vbNewLine & vbNewLine & _
               "We have received your e-mail " & vbNewLine & _
               "and either there is no attachment or " & vbNewLine & _
               "at least one of the attachments are invalid." & vbNewLine & vbNewLine

另请参阅此处Skip attachments in signatures