» Posted on March 7 2015 in Code
We run a Professional Photo Lab (in San Diego) and while we have a very powerful online ordering system called ROES, and also have a Simple File Upload form for our customers to use to order prints, we still end up with some people who just want to email us their images as attachments.
Attachments are a pain, especially when there are multiple files attached. You have to save one, which asks you where you want to save, then you have to browse out to it, create a folder etc.
Recently I saw a cool Dropbox plugin that allowed for this same sort of thing - you got an email address from the vendor, and it would harvest and save any attachments along with the body of the email to a folder in your Dropbox. I liked the idea, so I decided to see if I could get a Macro in Outlook to do more or less the same thing. I like this better than the Dropbox plugin as I won’t have to use a 3rd party email address, and I don’t have to use Dropbox.
Goal: Check incoming email for attachments, if there are any, save them to a specific folder in a sub folder by Sender Name, then in another sub folder by Date + subject line, and include a copy of the entire email as a txt file. It also needs to be somewhat bulletproof so we need to check for and remove any special characters that may be in the Subject or Sender Name, since both of those will be in the folder structure. Just for good measure it needs to save each filename with a sequence prefix so they are unique (within a single email).
I found lots of info on the web, though none of what I found did exactly what I wanted. These sites were very helpful:
http://www.pixelchef.net/content/rule-autosave-attachment-outlook
This site had most of what I needed, especially code that was in one of the comments.
Stackoverflow post
This helped expand on the pixelchef post above, both the OP’s own answer and especially the link to slipstick about Macros and security and why it worked at first and then stopped.
http://www.slipstick.com/developer/how-to-use-outlooks-vba-editor/
Without this I am not sure I would have ever figured it all out.
Stackoverflow post on replacing illegal characters
This post had a way to strip potential illegal characters out of a string in VBA. I used this to strip potential illegal characters out of the Subject and Sender Name since I am appending them to folder names.
Here is what I did to get all of this to work. This is with Outlook 2010 on a Windows 7 computer. Please keep in mind I am not an expert at VBA or Macros, I just like to obsess over things like this until they do what I want.
Alt + F11
this will open up the VBA editor for Macros in Outlook.Root_Folder = "D:\Emails" ' sets the parent folder to save to
and change the “D:\Emails” part to whatever the path/name of the parent folder you want to use is. This folder must already exist. Make sure you keep the quotes around it. Make sure it is a valid path. I think if there are spaces in the path or it is on a network drive it will work, but you will have to research how to write the correct path.Public Sub Save_Attach_To_Disk(itm As Outlook.MailItem)
' note the use of quotes around static things like / or - or emait.txt while declared variables like Root_Folder do not have them
' if you get an error - object expected it might be because you didn't put quotes around something static
' if this runs only once but then doesn't work it is because of macro security settings are not allowing it - there is no error shown
' only process emails that have attachments
If (itm.Attachments.Count >= 1) Then
Dim objAtt As Outlook.Attachment
' prep date format of sub folder
Dim dateFormat_target_folder
dateFormat_target_folder = Format(itm.ReceivedTime, "yyyy-mm-dd")
Dim Root_Folder As String
Root_Folder = "D:\Emails" ' sets the parent folder to save to
'add sub folder with sender name, checking for illegal characters
Dim saveFolder_Root2 As String
strSenderName = itm.senderName
sreplace = "_"
'create an array to loop through and replace any potential illegal characters
For Each mychar In Array("/", "\", "^", "*", "%", "$", "#", "@", "~", "`", "{", "}", "[", "]", "|", ";", ":", ",", ".", "'", "+", "=", "?", "!", " ", Chr(34), "<", ">", "¦")
strSenderName = Replace(strSenderName, mychar, sreplace)
Next mychar
saveFolder_Root2 = Root_Folder & "\" & strSenderName
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
' check and see if sub folder with sender name exisits, if not create it
If Not objFSO.FolderExists(saveFolder_Root2) Then
objFSO.CreateFolder (saveFolder_Root2)
End If
''''''''''''''''''''''
'add sub-sub folder with date and subject
Dim saveFolder_bySender_date As String
strSubject = itm.Subject
sreplace2 = "_"
'create an array to loop through and replace any potential illegal characters
For Each mychar2 In Array("/", "\", "^", "*", "%", "$", "#", "@", "~", "`", "{", "}", "[", "]", "|", ";", ":", ",", ".", "'", "+", "=", "?", "!", " ", Chr(34), "<", ">", "¦")
strSubject = Replace(strSubject, mychar2, sreplace2)
Next mychar2
saveFolder_bySender_date = saveFolder_Root2 & "\" & dateFormat_target_folder & "-" & strSubject 'adds subject to folder name
' check and see if sub-sub folder with date exists, under sender name, if not create it
If Not objFSO.FolderExists(saveFolder_bySender_date) Then
objFSO.CreateFolder (saveFolder_bySender_date)
End If
''''''''''''''''''''''
' loop thru and save each attachment to folder, ading a sequence prefix in case they managed to send a bunch with the same name
Dim i As Integer
i = 0
For Each objAtt In itm.Attachments
i = i + 1
objAtt.SaveAsFile saveFolder_bySender_date & "\" & "(" & i & ")-" & objAtt.DisplayName
' clear object after saving each one
Set objAtt = Nothing
Next 'go to next attachment
' save a copy of the email to the same folder
itm.SaveAs saveFolder_bySender_date & "\" & "email.txt", olTXT
'done
End If
End Sub
At this point you are mostly done, just a few more steps to create a Rule to run the script on incoming mail, and then also we need to change a security setting to allow self signed certs to run.
That’s it, you should now have a working rule that runs and checks incoming mail if it has attachments. If it does it should save them in a folder with the Senders Name and a sub folder with the date + subject.
Please note - if this works once, and then never again, it is probably because of Macro security, and you did no properly sign your macro or set the macro security properly. Outlook should pop up a warning box the first time you run your macro, say ok, and then it should run on its own even after rebooting/restarting.
I tried to comment the code as best I could, it should be fairly simple to change parts of it.
© 2015 ePhotoPros.com All Rights Reserved.