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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Read this checklist to learn more about the 15 things you should never include in an email signature.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 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

19 Experts available now in Live!

Get 1:1 Help Now