I am trying to create a macro that will take incoming emails with attachments and save them to a new network folder according to the attachment.

For example, i have an email coming from Joey Lawrence with the attachment name Whoa123.pdf. I want the macro to take that file and put it into a folder \\server\emailattachments\whoa123\whoa123.pdf

Then another email from Martin Lawrence with the attachment hi123.pdf, have the macro put that file into a folder \\server\emailattachments\hi123\hi123.pdf

Please advise, thank you!

John Darville18-Feb-201311:15

Anonymous18-Feb-201322:14
please

niton21-Feb-201317:31
' Set up a rule on incoming mail to "run a script"

Sub SaveAttachments_VariableFolder(MyMail As MailItem)

Dim Atmt As attachment

Dim FileName As StringDim lenName As LongDim strPathAdd As String

Const strPath As String = "C:\test\" ' set as desired

On Error Resume NextMkDir strPathOn Error GoTo 0

For Each Atmt In MyMail.Attachments

If (Right(Atmt, Len(Atmt) - InStrRev(Atmt, "."))) = "pdf" Then

lenName = InStrRev(Atmt, ".") - 1

' Trim possible spaces before the extension. ' A space at the end of the name created a problem with deleting the folder strPathAdd = strPath & Trim(Left(Atmt, lenName)) & "\"

On Error Resume Next MkDir strPathAdd On Error GoTo 0

FileName = strPathAdd & Atmt.FileName Atmt.SaveAsFile FileName

End If

Next Atmt

Set Atmt = Nothing

End Sub

John Darville22-Feb-201308:24
an unexpected error has occurred.Please note and export the following information.Macro name: SaveEmailAttachments ToFolderError Number: -2147221233Error Description: The attempted operation failed. An object could not be found.

John Darville22-Feb-201308:40
nevermind it worked, thank you so much!!!!!!!!!!!!!

John Darville22-Feb-201314:37
so this script is great but is there a way that we can have it create a folder with the name email address of the sender?

So if the attached file is coming from john@johninc.com, create a folder called john@johninc.com and put the attached file in there?