Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VBA code for outlook which checks for the categories of the emails and then saves them to file folders (same names with categories) on the drive if there is an attachment

Posted on 2010-08-30
3
351 Views
Last Modified: 2012-05-10
I need a vba code for MS Outlook which checks if there is a new email which is categorized with outlook categories (not necessarily all the categories, user should be able to specify which categories to check). Then it checks if the emails have attachments and if there is , the code saves the attachment to a file folder which has the same name as the category (If the file folder does not exist, the folder should be created).

Obviously, some emails might have several categories. I dont know what to do in those cases. We might try to save the attachments to each categories file folder. Or something else. I am open to suggestions on that issue.
0
Comment
Question by:awesomejohn19
  • 2
3 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 33570225
Hi, awesomejohn19.

Is the user going to set the categories once or are they going to be changing them a lot?
0
 

Author Comment

by:awesomejohn19
ID: 33576475
Hi BluedDevilFan,

They'll probably have a standard set of categories, but from time to time, they are likely to add new ones each month. So, they will change it.
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 33599408
Here is the code for doing this.  Follow these instructions to use it.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects
4.  If not already expanded, expand Modules
5.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
7.  Edit the code as needed.  I included comments wherever something needs to or can change
8.  Click the diskette icon on the toolbar to save the changes
9.  Close the VB Editor
10. Create a rule that fires for these messages
11. Set the rule's action to "run a script" and select this script as the one to run
Sub SaveAttachmentToCategoryFolder(Item As Outlook.MailItem)
    'On the next line change the path to the root folder that the categorical folders appear under'
    Const ROOT_PATH = "C:\eeTesting\"
    Dim arrCats As Variant, varCat As Variant, olkAttachment As Outlook.Attachment
    If Item.Attachments.count > 0 Then
        If Len(Item.Categories) > 0 Then
            arrCats = Split(Item.Categories, ",")
            For Each varCat In arrCats
                Select Case varCat
                    'On the next line change the category names.  These are the categories that trigger saving attachments.'
                    Case "Cat1", "Cat2", "Cat3"
                        For Each olkAttachment In Item.Attachments
                            olkAttachment.SaveAsFile ROOT_PATH & varCat & "\" & olkAttachment.FileName
                        Next
                End Select
            Next
        End If
    End If
    Set olkAttachment = Nothing
End Sub

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question