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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Learn more about how the humble email signature can be used as more than just an electronic business card. When used correctly, a signature can easily be tailored for different purposes by different departments within an organization.
Ransomware continues to be a growing problem for both personal and business users alike and Antivirus companies are still struggling to find a reliable way to protect you from this dangerous threat.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now