Script to move emails to a folder based on various possible keywords

Status
Not open for further replies.

outlookhelp

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
hi all

i have some code that i need a bit of help with - what i am trying to do is move emails from my Inbox to other folders based on keywords. The code i have moves an email to a folder called TEST if it contains the keyword ala in the subject line - i would like to know how i can add more keywords to the code (for example search for ala or apa or IES Pty Ltd) here is the code i have:

Sub MovePLA(Item As Outlook.MailItem)
With Item
If InStr(1, LCase(.Subject), "ala ") > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
End If
End With
End Sub

Also is there any way that i can search both the subject and body of the email for those keywords?

Thanks!
Deb
 

outlookhelp

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
Great thank you so much that worked ! i now have another little problem though :(

My code picks up the keyword whether it is standalone or whether it is within another word - so if i specify the keyword to be 'ala' it picks up 'ala' but also 'australasian' which is what i don't want. i would like ti to only pick up 'ala' as a standalone keyword . i tried adding a space to the end of 'ala' in the code but that does nothing. So my code is now:

Sub AlaSubject(Item As Outlook.MailItem)
With Item
If InStr(1, LCase(.Subject), "ala") > 0 Or InStr(1, LCase(.Subject), "sos") > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
End If
If InStr(1, LCase(.Body), "ala") > 0 Or InStr(1, LCase(.Body), "sos") Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
End If
End With
End Sub

Would you be able to advise how i make that code move only emails with the standalone keywords 'ala' and 'sos' but not words like australasian or sostenuto?

Thanks!
Deb
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
" ala " could do it.

BTW, your could is not what I suggested. Use ElseIf instead of two IFs else it could try to move the same item twice.
 

outlookhelp

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
Gosh now it doesn't work at all :( I modified to the following and it doesn't pick up an email with 'this is for people from Chile' in the subject line :( As it's looking for the exact match 'Chile' i thought it should find it and move it to the TEST folder?

Sub MultipleIfs(Item As Outlook.MailItem)
With Item
If InStr(1, LCase(.Subject), "Argentina") > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Subject), "Japan") > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Subject), "China") > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Subject), "Chile") > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Subject), "Spain") > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
End If
End With
End Sub

Thank yo so much for your help so far !
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
The LCase function converts a string to its lower cases. And "a..." is not equal "A...". If you pass vbTextCompare as the fourth parameter to Instr, lower cases and upper cases will be treated equally.
 

outlookhelp

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
Excellent thank you so much ! It is working perfectly now ! Can i be greedy and last one last question? I now have my script looking for keywods in the subject and body..i have about 100 keywords i need to look up..is there any way that i can do that more efficiently? I don't mind typing out all the code but i am just wondering if there are other functions that may do it better and faster?
This is what my code looks like now for only 5 keywords:

Sub MultipleIfs(Item As Outlook.MailItem)
With Item
If InStr(1, LCase(.Subject), "Argentina", vbTextCompare) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Body), "Argentina", vbTextCompare) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Subject), "Japan", vbTextCompare) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Body), "Japan", vbTextCompare) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Subject), "China", vbTextCompare) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Body), "China", vbTextCompare) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Subject), "Chile", vbTextCompare) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Body), "Chile", vbTextCompare) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Subject), "Spain", vbTextCompare) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
ElseIf InStr(1, LCase(.Body), "Spain", vbTextCompare) > 0 Then
Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("TEST")
End If
End With
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Are they all using the same folder? You can use Select Case or an array to hold the words (and set a folder, if necessary). I'm not sure which is more efficient with that many words - maybe Michael knows. (Or knows of a better way to do it.)
 

outlookhelp

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
There are 3 folders:
Gold - 110 phrases and keywords
Platinum - 30 phrases and keywords
Registered - 200 phrases and keywords

Thanks!
Deb
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
I'd do it this way. Set the three folder variables to the target folders, and the keywords for folder(0) to keyword(0), etc. This allows to add more folder/keywords anytime.

Code:
dim folder(2) as folder
dim keyword(2) as variant
dim  i as long, y as long
dim s as string, b as string
dim done as boolean
 
keyword(0)=array(a,b,c)
...
set folder(0)=....
...
s=item.subject:b=item.body
for i=0 to 2
  for y=0 to ubound(v(i))
    if instr(1, s,keyword(i)(y),vbtextcompare) then
      item.move folder(i)
      done=true
    elseif instr(1, b,keyword(i)(y),vbtextcompare) then
       item.move folder(i)
      done=true
    endif
    if done then exit for
  next
  if done then exit for
next
 

outlookhelp

Member
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
Great ti will try this out ! Thanks so much for the help - greatly appreciated!
 
A

AJBoink

Could someone please help me with a similar code as above? I am new to scripting and i am finding it difficult to understand what different objects or methods are used for; but i am trying to refer outlook vba reference.
Could someone provide me the code for moving a mail (coming from a particular sender) to different folders, and then check the subjects of those mails(the condition has 'AND') based on which they will be moved to different other folders eventually
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
Q VBA Script to move item in secondary mailbox Outlook VBA and Custom Forms 2
D.Moore VB script to Digitaly Sign newly created outlook message Outlook VBA and Custom Forms 2
Aussie Rules Run a Script on an Incoming Email OK and then the Email reverts Outlook VBA and Custom Forms 0
D.Moore VBA script fail after Office 365 update Using Outlook 8
M Outlook 2013 Script Assistance - Save Opened Link with Subject Added Outlook VBA and Custom Forms 1
F Script for zip file attachment Outlook VBA and Custom Forms 1
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
Y Outlook 2013 Run A Script Outlook VBA and Custom Forms 4
Z Script to set account? Using Outlook 0
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
N VBA Script to Open highlighted e-mail and Edit Message Outlook VBA and Custom Forms 5
B Outlook rule run a Script doesn't work Outlook VBA and Custom Forms 1
J Calling a Public sub-routine from the script editor via VB script Outlook VBA and Custom Forms 4
K Outlook Archive to PST Files by Date Range VBA Script? Outlook VBA and Custom Forms 1
Peter H Williams Enable script containing VBA Outlook VBA and Custom Forms 12
H VB script in outlook form doesn't work anymore Outlook VBA and Custom Forms 2
A Script to fetch data from mails in restricted collection and sending them to excel Using Outlook 1
B Wanting to run a script that will filter any body that has a russian link in it. Outlook VBA and Custom Forms 5
Bri the Tech Guy Registry Tweak to make "Run a Script" Action Available Outlook VBA and Custom Forms 2
V VB script code to save a specific email attachment from a given email Outlook VBA and Custom Forms 14
Bri the Tech Guy Run Script rule not running for newly arriving messages Outlook VBA and Custom Forms 25
M Subject Line Automation - Trigger Script Delayed Outlook VBA and Custom Forms 2
Q Script to create a pst file for Archiving Using Outlook 1
Vijay Error in rule- Run a script Using Outlook 1
R VBA Script Quick Parts Using Outlook 1
Vijay Run script doesn't work in outlook Using Outlook 1
Diane Poremsky Run a Script Rule: Send a New Message when a Message Arrives New Slipstick.com Articles 2
F Avoid sending duplicate using Outlook script Outlook VBA and Custom Forms 2
oliv- How to Run a Script IN AN ADDIN with Outlook's Rules and Alerts Outlook VBA and Custom Forms 2
L Run a Script Rule doesn't work Using Outlook 5
N Outlook script to forward emails based on senders' address Outlook VBA and Custom Forms 2
S using script rule to save attachments on arrival Outlook 2010 Outlook VBA and Custom Forms 9
X Outlook script to run excel data Outlook VBA and Custom Forms 1
N VBA Script to Send Automatic Emails from Outlook 2010 Outlook VBA and Custom Forms 1
Davzell Change default pop3 account with script, prf or registery ? Outlook VBA and Custom Forms 0
D RUN SCRIPT WHEN OUTLOOK IS CLOSE Outlook VBA and Custom Forms 1
L Cannot run script from rule Outlook VBA and Custom Forms 7
O modify vba to run it as script rule Outlook VBA and Custom Forms 8
D Script to parse email and set a task Outlook VBA and Custom Forms 1
L Moving Message Class email via script and Rule Outlook VBA and Custom Forms 3
P How many subs can run in one outlook VBA script Using Outlook 5
H Customizing "Send to" Attachment Script Outlook VBA and Custom Forms 5
G Script in rule to send to multiple emails found in message bo Outlook VBA and Custom Forms 11
Xueying run a script in rules, first time succeed, failed afterwards Outlook VBA and Custom Forms 3
A Creating archive rule on the clients by script/ Outlook VBA and Custom Forms 3
J VBS Script (macro) for word to open Outlook template. Outlook VBA and Custom Forms 2
J Email Parsing VBA Script for Outlook - NEEDED Outlook VBA and Custom Forms 7
P Vba script including macro appears in rules but wont run Outlook VBA and Custom Forms 6
R Adding vba to script list Outlook VBA and Custom Forms 4
Jeff Rott Diane Question on "Use in a Run a Script Rule" Outlook VBA and Custom Forms 1

Similar threads

Top