PDA

View Full Version : Save attachment but excluding all jpg and png file



imwytheee
08-05-2020, 10:05 PM
Hi all

I tried looking on the post already but seem couldnt have an solution.


I would like to add something or below to the code which work at current. I would like to exclude all jpg and png file type.

'want to delete image file

If UCase(Right(objAtt.FileName, 3)) <> "PNG" And UCase(Right(objAtt.FileName, 3)) <> "GIF" Then

strFile = strPath & objAtt.FileName

objAtt.SaveAsFile strFile

objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName

fso.DeleteFile strFile

End If



Im currently having this code work upon receiving each email and autosave the attachment.


Public Sub Test(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String


From = itm.Sendername


saveFolder = "C:\Users\abcbac\Documents\Emailattachment"
For Each objAtt In itm.Attachments


stFileName = saveFolder & "" & Format$(itm.CreationTime, "yyyy.mm.dd hhmm - ") & From & " - " & objAtt.DisplayName
i = 1
JumpHere:
If Dir(stFileName) = "" Then
objAtt.SaveAsFile stFileName
Else
i = i + 1
stFileName = saveFolder & "" & Format$(itm.CreationTime, "yyyy.mm.dd hhmm - ") & From & (" - " & i & " - ") & objAtt.DisplayName
GoTo JumpHere
End If


Set objAtt = Nothing
Next

gmayor
08-06-2020, 02:25 AM
Your proposed code saves the attachments then attempts to delete the saved attachments. I am not sure what the point of that would be?

I suspect what you are looking for is


Public Sub Test(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim stFilename As String
Dim From As String
Dim i As Integer

From = itm.SenderName

saveFolder = "C:\Users\abcbac\Documents\Emailattachment"
For Each objAtt In itm.Attachments
If UCase(Right(objAtt.fileName, 3)) <> "PNG" And UCase(Right(objAtt.fileName, 3)) <> "JPG" Then
stFilename = saveFolder & "" & Format$(itm.CreationTime, "yyyy.mm.dd hhmm - ") & From & " - " & objAtt.fileName
i = 1
JumpHere:
If Dir(stFilename) = "" Then
objAtt.SaveAsFile stFilename
Else
i = i + 1
stFilename = saveFolder & "" & Format$(itm.CreationTime, "yyyy.mm.dd hhmm - ") & From & (" - " & i & " - ") & objAtt.fileName
GoTo JumpHere
End If
End If
Set objAtt = Nothing
Next
End Sub