Custom VBA to sort emails into folders

aorox

Member
Outlook version
Outlook 2016 64 bit
Email Account
Outlook.com (as MS Exchange)
Hey there... am new here :)

I am trying to make a VBA which can automatically download emails (and their attachments) to certain folders based on a word or code mentioned in the email. For example, I have a folder on my harddrive located at "C:\Uni-Work-2019\2259\Emails", and receive a lot of emails for an assessment which is referred to as '2259'. Everyone in the group uses the number either in the subject line or on the first line of every email. I have another folder "C:\Uni-Work-2019\2260\Emails", etc. I am trying to make a VBA to download everything into those specific folders, however I am finding this quite difficult though as I have no programming experience, and am just trying to follow along with You Tube guides... :(

From what I've read on the internet and watched on You Tube, I came up with the following, but not too sure why it's not working. Any tips would be highly appreciated. Please note I am not posting this expecting someone to write the code for me - I do also want to learn how it's done myself :p

Code:
Private WithEvents InboxItems As Outlook.Items
Sub Application_Startup()
Dim xNameSpace As Outlook.NameSpace
Set xNameSpace = Outlook.Application.Session
Set InboxItems = xNameSpace.GetDefaultFolder(olFolderInbox).Items
End Sub
 
Private Sub InboxItems_ItemAdd(ByVal objItem As Object)
Dim FSO
Dim xMailItem As Outlook.MailItem
Dim xFilePath As String
Dim xRegEx
Dim xFileName As String
On Error Resume Next
xFilePath = CreateObject("WScript.Shell").SpecialFolders(16)
xFilePath = xFilePath & "\C:\Uni-Work-2019\2259\Emails"
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(xFilePath) = False Then
FSO.CreateFolder (xFilePath)
End If
Set xRegEx = CreateObject("vbscript.regexp")
xRegEx.Global = True
xRegEx.IgnoreCase = False
xRegEx.Pattern = "\||\/|\<|\>|""|:|\*|\\|\?"
If objItem.Class = olMail Then
Set xMailItem = objItem
xFileName = xRegEx.Replace(xMailItem.Subject, "")
xMailItem.SaveAs xFilePath & "\" & xFileName & ".html", olHTML
End If
 Set Atts = Item.Attachments
 
    If Atts.Count > 0 Then
       For Each Att In Atts
           If InStr(LCase(Att.FileName), "2259") > 0 Then
              strPath = "C:\Uni-Work-2019\2259\Emails"
              strName = NewMail.Subject & " " & Chr(45) & " " & Att.FileName
              Att.SaveAsFile strPath & strName
           End If
       Next
    End If
Exit Sub
End Sub
Kind regards,
 
Thread starter Similar threads Forum Replies Date
S Reference Custom Fields with VBA Outlook VBA and Custom Forms 2
D Using a VBA Custom Form to Send Reoccurring Email Upon Task Completion Outlook VBA and Custom Forms 4
D create an html table in outlook custom form 2010 using vba in MsAccess Outlook VBA and Custom Forms 7
D Change sender name outlook vba 2010 Custom Userform Outlook VBA and Custom Forms 1
G Adding a contact to Outlook with a custom form using Access VBA Outlook VBA and Custom Forms 1
D Creating custom view with VBA Outlook VBA and Custom Forms 2
G Message template / custom forms and VBA Help needed - inserting info into table Outlook VBA and Custom Forms 3
R How To Refer To Custom Folder in VBA Outlook VBA and Custom Forms 1
J Outlook custom form - VBS call VBA macro Outlook VBA and Custom Forms 3
S Outlook Email Help: Select custom voting button options VBA Outlook VBA and Custom Forms 1
H add an appointment for a custom calendar, vba excel Outlook VBA and Custom Forms 2
S Custom VBA forms in Outlook--how to call w/macro? Outlook VBA and Custom Forms 1
C Use VBA editor on my custom OL form Outlook VBA and Custom Forms 7
K UDF with formula not showing on Calendar custom view. Outlook VBA and Custom Forms 0
S Create a clickable custom column field Outlook VBA and Custom Forms 0
I Error saving screenshots in a custom form in outlook 2016, outlook 365 - ok in outlook 2013, outlook 2010 Outlook VBA and Custom Forms 5
M VbScript for Command Button on Contacts Custom Form Using Outlook 1
G Other users can't see P.2 with custom fields in Form Outlook VBA and Custom Forms 0
O Create a custom contact form - questions before messing things up... Outlook VBA and Custom Forms 4
L Custom Form Tutoral? Outlook VBA and Custom Forms 6
D Lost Access to Custom Form Outlook VBA and Custom Forms 4
M vCard does not have user-defined fields from my custom contact form (365) Using Outlook 1
S Outlook Custom Form Scripting only working when clicking on "Run this form" Outlook VBA and Custom Forms 2
Victor_50 Outlook 2013 Custom Contact Form starts with "E-mail 2" Outlook VBA and Custom Forms 2
C Custom Form (seperate layout pages and message reading pane) Outlook VBA and Custom Forms 0
C Reading Pane for Custom Form Outlook VBA and Custom Forms 2
M Custom Calendar Print Suggestions? Using Outlook 0
K Custom Category Colors Using Outlook 2
N Custom Form Controls Not Visible To Recipient Outlook VBA and Custom Forms 3
E To convert imported data to custom fields in Task list Outlook VBA and Custom Forms 1
Randy Redekopp How To Merge Contact Info to Email Custom Form Template Using Outlook 2
D Problem with custom form including _DocSiteControl1 Outlook VBA and Custom Forms 0
C Custom Outlook Form - Populate Information from Radio Button / Check Box Using Outlook 0
H Custom Signature Not Displayed When Account Selected Outlook VBA and Custom Forms 10
E Custom formula for columns Using Outlook 3
W Message class changes of a custom form changes to the default form Using Outlook 2
A Possible to hide ribbon with custom appointment form? Outlook VBA and Custom Forms 3
S Custom Form, copy user field data to message body Outlook VBA and Custom Forms 12
Andrew Quirl Custom form to route requests based on input criteria Outlook VBA and Custom Forms 1
W Setting up a custom form Outlook VBA and Custom Forms 2
A Greyed out checkbox in custom form Outlook VBA and Custom Forms 4
Z Outlook Custom Form: Adding Dropdown(Project Code) at the end of subject Outlook VBA and Custom Forms 0
Z Adding dropdown list using custom form Outlook VBA and Custom Forms 7
G Entered data in custom field goes in card and does not stay in list view Outlook VBA and Custom Forms 1
witzker Correct Format of custom Yes/No Checkboxes Outlook VBA and Custom Forms 0
O Searching for Custom Flags Using Outlook 3
J autocomplete function in custom form Using Outlook 1
S Custom user fields in received messages Outlook VBA and Custom Forms 1
J Backup .OST - Custom Contact Forms, Defined Fields, Notes Using Outlook 1
D Custom form with html hyperlink Outlook VBA and Custom Forms 7
Similar threads


















































Top