Consulting

Results 1 to 7 of 7

Thread: Screening incoming mail

  1. #1
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    4
    Location

    Screening incoming mail

    Hi,

    I receive daily an email with 3 attached files, one of them having the ext .TXT.

    The following code saves in a folder the 3 received attachments using a Rule.

    I’d like to modify the code to store only the .TXT file.

    How do I go by?
    TIA
    EDIT: Outlook 2007


    Public Sub saveAttach(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    saveFolder = "c:\myAttFiles\"
         For Each objAtt In itm.Attachments
              objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
              Set objAtt = Nothing
         Next
    End Sub
    Last edited by ebolisa; 03-07-2017 at 03:06 AM.

  2. #2
    Public Sub saveAttach(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
        saveFolder = "c:\myAttFiles\"
        For Each objAtt In itm.Attachments
            If Right(LCase(objAtt.fileName), 4) = ".txt" Then
                objAtt.SaveAsFile saveFolder & "\" & objAtt.fileName
            End If
        Next
        Set objAtt = Nothing
    End Sub
    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
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    4
    Location
    gmayor, thanks for getting back to me.
    I tried your suggestion but still not filtering out the non wanted extensions. I changed the .txt to .TXT with no avail (just in case).
    Nor did
    UCase(objAtt.fileName), 4) = ".TXT"
    helped.

  4. #4
    It works fine here. Test it against a message in your inbox that has at least one txt format attachment with

    Sub TestMacro()
    Dim olMsg As MailItem
        On Error Resume Next
        Set olMsg = ActiveExplorer.Selection.Item(1)
        saveAttach olMsg
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    4
    Location
    Cap.jpgI get a compiling error. It halts stating that there's no Sub or Function

    EDIT:
    My bad. It ran and saved the 3 files and not only one.
    Last edited by ebolisa; 03-08-2017 at 08:21 AM.

  6. #6
    SaveAttach is the macro in my previous reply. It should be present for the test macro to find.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    4
    Location
    Ok, I select the test email having three files with different extensions one of them being .TXT, go to Tools, Macro, Macro, Activate Macros and execute TestMacro.
    I then go the folder and I find the three files whereas should be only one with the .TXT extension. So, the test macro is doing its job but, for some reason, the filtering is failing.

Posting Permissions

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