PDA

View Full Version : [SOLVED:] How to save attachments with certain words in the attachment name to a folder (HDD)



therion17
04-05-2019, 12:53 PM
Hi, I'm trying to create a script to read attachment names in a folder of emails and only save the attachments with specific words in the name. I would like to attach this to a rule so can manually run this through whichever folder I set. Right now I've been using this code, however it saves ALL attachments:

Public Sub AIG(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
sSaveFolder = "C:\Test"
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
Next
End Sub

Thank you in advance!

gmayor
04-05-2019, 08:51 PM
Just add a simple condition e.g.

Public Sub AIG(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
Dim strText As String
strText = "Text to find"
sSaveFolder = "C:\Test\"
For Each oAttachment In MItem.Attachments
If InStr(1, oAttachment.fileName, strText) > 0 Then
oAttachment.SaveAsFile sSaveFolder & oAttachment.fileName
End If
Next oAttachment
Set oAttachment = Nothing
End SubNote that the path must end in a folder separator character and it is probably better to search the filename than the displayname.

therion17
04-08-2019, 08:08 AM
Ah that makes sense and it works brilliantly. Thanks so much gmayor! I'm new to this forum is there a way to mark as solved / give you a +1 or something?