Outlook VBA for Email Filter

Status
Not open for further replies.

jasonm123

New Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Hi All,

I need a VBA code with the below requirements.

All my emails have a specific Subject line pattern. e.g. if the request is for Jason.. The subject line is "Issue with Desktop [JAS].. There are 10-15 such names and each one of them has a short name in the subject line of the email.. For each of those names there is a folder. I would want subject lines with [JAS] to move to JAS folder and so on..

Is this possible.. Please help.. I need this urgently..

Thanks

Jason
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This should work - although the subject might need tweaked a little


it assumes the folders are under under the currently selected folder. as written it works on the semected messages - this makes it easy to test and it can be changed after you are sure it works.

Code:
Public Sub MoveSelectedMessages()
    Dim objOutlook As Outlook.Application
    Dim objNamespace As Outlook.NameSpace
    Dim objDestFolder As Outlook.MAPIFolder
    Dim objSourceFolder As Outlook.Folder
    Dim currentExplorer As Explorer
    Dim Selection As Selection
    Dim obj As Object
    
    Dim objVariant As Variant
    Dim strSubject As string
    Dim strDestFolder As String
  
    Set objOutlook = Application
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set currentExplorer = objOutlook.ActiveExplorer
    Set Selection = currentExplorer.Selection
    Set objSourceFolder = currentExplorer.CurrentFolder
    
    For Each obj In Selection
        Set objVariant = obj
    If objVariant.Class = olMail Then

strsubject = Right(objVariant.subject, Len(objVariant.subject) - InStr(1, objVariant.subject, "["))
strsubject = left(strsubject,len(strsubject - 1))
On Error Resume Next
Set objDestFolder = objSourceFolder.Folders(strsubject)
 
            objVariant.Move objDestFolder
            'count the # of items moved
            Set objDestFolder = Nothing
        End If
    End If
        Err.Clear
    Next
    Set currentExplorer = Nothing
    Set obj = Nothing
    Set Selection = Nothing
    Set objOutlook = Nothing
    Set objNamespace = Nothing
    Set objSourceFolder = Nothing
End Sub
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
N Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
J VBA Outlook : Subject line : Cut and Paste name to heading , number to very end of the body of Email Outlook VBA and Custom Forms 1
J Open an outlook email by Subject on MS Access linked table with VBA Outlook VBA and Custom Forms 10
P Outlook 2007 Email Categorization using VBA Outlook VBA and Custom Forms 1
Norbert VBA Flag that would indicate the email was sent from Outlook to Access. Outlook VBA and Custom Forms 2
Z Default VBA code for extracting data from email (Outlook) to Excel? Outlook VBA and Custom Forms 1
D Outlook VBA to open Excel attachment and send recipient's email address to a workbook cell? Using Outlook 4
J Email Parsing VBA Script for Outlook - NEEDED Outlook VBA and Custom Forms 7
H Problems With Outlook 2013 VBA To Send and Print an email Outlook VBA and Custom Forms 1
D Redemption? Need rapid pull of Outlook Contacts, email + notes for VBA Using Outlook 1
S Outlook Email Help: Select custom voting button options VBA Outlook VBA and Custom Forms 1
S Email Help: Sending Outlook email from Excel VBA Outlook VBA and Custom Forms 6
G Using VBA to load the CC line on an Outlook email Outlook VBA and Custom Forms 2
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
G Outlook VBA and Google Calendar ("Events") Outlook VBA and Custom Forms 1
B Advanced Search in MS Outlook by VBA and SQL Outlook VBA and Custom Forms 2
K Outlook Archive to PST Files by Date Range VBA Script? Outlook VBA and Custom Forms 1
J Help Please!!! Outlook 2016 - VBA Macro for replying with attachment in meeting invite Outlook VBA and Custom Forms 9
S Find a cell value in excel using outlook vba Using Outlook 1
J Execute Add-In Button from VBA Outlook 2016 Outlook VBA and Custom Forms 1
D create an html table in outlook custom form 2010 using vba in MsAccess Outlook VBA and Custom Forms 7
M Slow VBA macro in Outlook Outlook VBA and Custom Forms 5
T Outlook AntiSpam with VBA Outlook VBA and Custom Forms 1
F "Move to" O365 feature to Outlook client via VBA Outlook VBA and Custom Forms 4
B query outlook using vba Outlook VBA and Custom Forms 13
J VBA to switch Outlook online/offline Outlook VBA and Custom Forms 4
M VBA to change flag status in outlook contact item Outlook VBA and Custom Forms 3
T VBA outlook, detect priority emails Outlook VBA and Custom Forms 5
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
stephen li VBA Outlook send mail automatically by specified outlook mail box Outlook VBA and Custom Forms 1
S Outlook VBA Contacts Notes Outlook VBA and Custom Forms 0
O VBA to Run Font Change on Outlook Startup Outlook VBA and Custom Forms 4
O VBA or other solution for Outlook tasks to OneNote Outlook VBA and Custom Forms 0
S Automatically selecting folders and deleting messages in Outlook VBA Outlook VBA and Custom Forms 7
D Creating an outlook session from Access vba but run silently. With A specific profile Outlook VBA and Custom Forms 1
Diane Poremsky Outlook VBA: Use a Text File to Populate a ListBox New Slipstick.com Articles 0
C Saving Outlook attachments and links to attachments with VBA Outlook VBA and Custom Forms 2
Diane Poremsky Outlook VBA: Work with Open Item or Selected Item New Slipstick.com Articles 0
Diane Poremsky Working with VBA and non-default Outlook Folders New Slipstick.com Articles 0
C Outlook VBA to set current account Outlook VBA and Custom Forms 1
Diane Poremsky Use VBA to create an Outlook Search Folder for Sender New Slipstick.com Articles 0

Similar threads

Top