Filter attachments

I have a script that sends a notification when no attachment was found in an email. Is it possible to check the file type of the attachment and send a notification if the file type is not the one you want.

Got a code like this.

    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

      

+3


source to share


2 answers


Is it possible to check the file type of the attachment and send a notification if the file type is not the one you want.

Yes it is.



The Attachment class provides a FileName property that returns a string representing the file name of the attachment.

+1


source


I would use a select case function which will perform much better


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

      

Edit comments



for multiple lines try

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

      

also see here how to Skip attachments in signature

+1


source







All Articles