Outlook rule to save attachments to a web directory
If you are like me, you get a lot of email messages with attachments in your Outlook email account. Wouldn’t it be nice to automate the migration of these attachments to a folder of your choosing? Well, you can, and we will show you how.
First off, it pays to research your particular topic. If you are looking for an excellent primer, you can find some good information in the following book:
At my job, we have web pages that are automatically generated and emailed to us. We are tired of manually moving these files to the appropriate web directory (in this case, samba shares that are mapped with drive letters assigned to them). So, the logical solution is to use Outlook Rules to push the attachments to the mapped drive. However, Outlook rules have one limitation: you can move messages to specific folders, but moving attachments is another matter that is almost impossible to perform with the default Outlook Rules wizard. So, we are going to use Outlook’s built in Visual Basic Editor to create some custom code to drive the attachment migration. Here is how we did it:
- Open up Outlook. I am using Outlook 2007, but this should also work in Outlook 2003. Go to Tools>Macro>Visual Basic Editor.
- With your project folder highlighted (in the top left, in the PROJECT pane; I just used the default project), right click and choose Insert>Module. Copy and paste the following code into the main window of the editor:
[vb]Sub SaveAttachmentsToDisk(Item As Outlook.MailItem)
Dim olkFolder As Outlook.MAPIFolder, _
olkAttachment As Outlook.Attachment, _
objFSO As Object, _
strRootFolderPath As String, _
strFilename As String, _
intCount As Integer
‘Change the following path to match your environment
strRootFolderPath = "z:\www\departments\webreports\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set olkFolder = Application.ActiveExplorer.CurrentFolder
If Item.Attachments.Count > 0 Then
For Each olkAttachment In Item.Attachments
If objFSO.GetExtensionName(LCase(olkAttachment.FileName)) = "htm" Then
strFilename = olkAttachment.FileName
intCount = 0
Do While True
If objFSO.FileExists(strRootFolderPath & strFilename) Then
intCount = intCount + 1
objFSO.deletefile (strRootFolderPath & strFilename)
Else
Exit Do
End If
Loop
olkAttachment.SaveAsFile strRootFolderPath & strFilename
End If
Next
End If
Set objFSO = Nothing
Set olkAttachment = Nothing
Set olkFolder = Nothing
End Sub[/vb] - You will need to ensure that you have the proper security level set in order to properly process the script. In Outlook, go to Tools>Macro> Security. I chose No security check for macros. This might be too loose of a restriction for your environment; if so, try the next highest setting.
- Create a new Outlook rule (Tools>Rules and Alerts) to reflect your changes. My rule looks for new messages from a specific email address and has an attachment (the web file that I want to move), moves the message to a specific folder (so I can have a backup of the message/attachment), then runs the module/script above to move the web file to the appropriate samba share. Here is what my Rule Description looks like:
Apply this rule after the message arrives from joeuser@email.com and which has an attachment and on this machine only move it to the WEBBACKUP folder and run Project1.SaveAttachmentsToDisk - Hit Apply and OK to save your rule. A couple of caveats: this is a client-side rule, so you must keep Outlook running in order for the rule to process. Also, the code will overwrite any file (in my case, in the target samba share) that has the same name as the attachment. If you only wish to make a copy, you can append a number to the attachment name. To do so, replace this line of code:
objFSO.deletefile (strRootFolderPath & strFilename)with
strFilename = "Copy (" & intCount & ") of " & olkAttachment.FileNameThat’s it!
Thanks for the helpful post.
I cannot get it to work however. First of all, in your script, I believe you don’t mean to have “>” and “&:” but “>” and “&” respectively.
I am at work, using Exchange cached mode, attempting to save file attachments (TIFF files) into “c:\faxes\”. I have macro security set to none. My rule looks *exactly* like yours.
I did do the code line replacement, because I don’t want to delete older files of the same name.
Any ideas of how I might troubleshoot this?
Correct on the script special character conversion (thanks for pointing this out); I updated the code example to reflect this.
As for troubleshooting, did you change line 14 (htm) file extension to reflect your tiff files (change to tif)? I am also under the assumption that you commented out line 20 in order to avoid the file deletion.
I would recommend adding a msgbox to the script to ensure that the script is even getting called and running. For example: MsgBox(”testing the script”). If the MsgBox displays when you run the rule, the code is getting called.
You might also want to try a new rule and a test script…one that just moves files that are smaller than your tif files (txt, for example). If these work, then it could be a file size limitation on your large tiff files.
Please feel free to share your code as well, and we can troubleshoot together.
I tried using your VB script to save attachements. I pasted it into VB editor from the [vb] to [/vb] but when I compile and run it highlights the first word in the script “sub” and gives a complile error “expected :expression”
@Norm
Hi, Norm.
Make sure you do not include the [vb] and [/vb] in your module, and also make sure to update your strRootFolderPath to reflect your environment/network drive. Everything else should work as-is (note: you might have to change the ‘htm’ extension if you are wanting to copy another filetype).
such a useful post. thanks for saving me hours of stuffing around trying to work out how to do this.
It worked the first time for me. Much appreciated.
Ben
Glad it was useful, Ben!
I used this code yesterday and it worked miracles. Unfortunately, after restarting Outlook this morning, it no longer saves the files. It does move them to the specified folder, it just doesn’t save the attachment. Anyone have any ideas? Thanks.
Lacy
Lacy,
I have experienced this problem before, and, although I have not completely figured it out, I did come up with a bit of a work-around. Usually, if this happens, I go into the Outlook rule properties, manually rerun the rule (highlight rule, choose CHANGE RULE>Edit Rule Settings, hit NEXT until you get to “Finish Rule Setup,” check RUN THIS RULE NOW ON MESSAGES ALREADY IN INBOX, and then “Finish”), and it usually starts working again (automated). I will keep digging for a better solution, though.
This is sooooo cool. This is going to save me a bunch of head aches. Thanks a bunch!!!
Just assistance to others, if it does not work (but does compile etc) ensure you’ve not accidentally dropped the trailing ‘\’ from the path otherwise it gets confused.
The script won’t be an option when building the rule unless you remove the [vb] and [\vb].
Wonderful code…thanks a bunch!@Thantos
When I run it, I get a message box from outlook that says “Rules in Error The script “” doesn’t exist or is invalid.”
Any ideas what I’ve done wrong?
Nevermind. It works great. Somehow I renamed the main “Module1″. Thanks for publishing this.