Home > Office 2007 > Outlook rule to save attachments to a web directory

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:

  1. Open up Outlook.  I am using Outlook 2007, but this should also work in Outlook 2003.  Go to Tools>Macro>Visual Basic Editor.
  2. 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]
  3. 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.
  4. 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
  5. 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.FileName 

    That’s it!

Thantos Office 2007

April 24th, 2009
  1. Eric
    May 20th, 2009 at 21:17 | #1

    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 “&amp:” 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?

  2. Thantos
    May 22nd, 2009 at 13:48 | #2

    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.

  3. Norm
    October 5th, 2009 at 22:11 | #3

    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”

  4. Thantos
    October 9th, 2009 at 14:34 | #4

    @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).

  5. October 23rd, 2009 at 00:45 | #5

    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

  6. Thantos
    October 23rd, 2009 at 13:18 | #6

    Glad it was useful, Ben!

  7. October 29th, 2009 at 17:29 | #7

    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

  8. Thantos
    November 2nd, 2009 at 01:08 | #8

    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.

  9. Duane
    December 14th, 2009 at 16:07 | #9

    This is sooooo cool. This is going to save me a bunch of head aches. Thanks a bunch!!!

  10. Leftnut
    December 23rd, 2009 at 07:05 | #10

    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.

  11. Jeff
    January 19th, 2010 at 18:48 | #11

    The script won’t be an option when building the rule unless you remove the [vb] and [\vb].

    Wonderful code…thanks a bunch!@Thantos

  12. Rich
    February 3rd, 2010 at 00:30 | #12

    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?

  13. Rich
    February 3rd, 2010 at 01:16 | #13

    Nevermind. It works great. Somehow I renamed the main “Module1″. Thanks for publishing this.

  1. No trackbacks yet.