r/vba•
WibblyWobblyy

Save attachment from an email in to a folder that changes every month

i 'm trying to get a VBA macro in Outlook that will save an email's attachment to a specific folder (that changes every month) and add the YYYYMM of prior month received to the file name.

The outlook rule identifies that an email header contains 'NTMR' from a person.

And when it does so, it runs the script where it saves the attachment in a folder.

So when the macro identifies the month of the email received, it saves in the folder that is one month behind. For instance:

email received on DD/04/17 as 'Here is the NTMR file for you', it will save the file in a folder within 201703 parent folder as NTMR - 201703

So the path of the file will be C:\Users\alitalh\Downloads\Test\201703\Source Files\NTMR 201703

I have come up with the follownig macro - please advise as To how i can fix it?

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim dateFormat As String
    saveFolder = "C:\Users\alitalh\Downloads\Test"
    dateFormat = Format(Now, "yyyymm" - 1, 1)

    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & dateFormat & "\" & "Source Files" & "\" & objAtt.DisplayName & dateFormat
        Set objAtt = Nothing
    Next
End Sub

how can i strip out the NTMR from the header and put it in the filename?

I have another macro that sets up the folder prior to the email so we don't need to create another folder

Posted Apr 26, 2017, 5:38 PM UTC

32
Loading comments…