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

Discussion in 'Outlook VBA and Custom Forms' started by outlookhelp, Jul 7, 2014.

  1. outlookhelp

    outlookhelp

    Member
    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
     
  2. Michael Bauer

    Michael Bauer

    Senior Member
    You could use something like this:
    Code (Text):
    Copy Source

    If Instr(...)>0 or Instr(...)>0 Then
    'move
    Elseif Instr(..)>0 or Instr(...)>0 Then
    'move
    End If
     
    outlookhelp likes this.
  3. outlookhelp

    outlookhelp

    Member
    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
     
  4. Michael Bauer

    Michael Bauer

    Senior Member
    " 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.
     
  5. outlookhelp

    outlookhelp

    Member
    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 !
     
  6. Michael Bauer

    Michael Bauer

    Senior Member
    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 likes this.
  7. outlookhelp

    outlookhelp

    Member
    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
     
  8. Diane Poremsky

    Diane Poremsky

    Senior Member
    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.)
     
  9. outlookhelp

    outlookhelp

    Member
    There are 3 folders:
    Gold - 110 phrases and keywords
    Platinum - 30 phrases and keywords
    Registered - 200 phrases and keywords

    Thanks!
    Deb
     
  10. Michael Bauer

    Michael Bauer

    Senior Member
    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 (Text):
    Copy Source

    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
     
    Diane Poremsky likes this.
  11. outlookhelp

    outlookhelp

    Member
    Great ti will try this out ! Thanks so much for the help - greatly appreciated!
     
  12. AJBoink

    AJBoink

    New Member
    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
     
Loading...

Share This Page