Reputation: 101
I have a script which sends a notification when no attachment was found on the email. Is it possible to check the file type of the attachment and send a notification if the file type is not the one needed.
Got the 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
Upvotes: 3
Views: 4164
Reputation: 12499
I would use select case function which will work 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 look here how to Skip attachments in signatures
Upvotes: 1
Reputation: 49395
Is it possible to check the file type of the attachment and send a notification if the filetype is not the one needed.
Yes, it is.
The Attachment class provides the FileName property which returns a string representing the file name of the attachment.
Upvotes: 1