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
347 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

778 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