Consulting

Results 1 to 3 of 3

Thread: How to save attachments with certain words in the attachment name to a folder (HDD)

  1. #1

    How to save attachments with certain words in the attachment name to a folder (HDD)

    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!

  2. #2
    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 Sub
    Note that the path must end in a folder separator character and it is probably better to search the filename than the displayname.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •