0

I am trying to use a regular expression to filter incoming messages by looking at the subject line and if it contains 6 consecutive digits, move it to a particular folder.

I found a script online which I have been trying to modify.

I want to place these emails in a folder called 'AMEX' which is a subfolder of the main Inbox.

Sub filter(Item As Outlook.MailItem)
    Dim ns As Outlook.NameSpace
    Dim MailDest As Outlook.Folder

    Set ns = Application.GetNamespace("MAPI")
    Set Reg1 = CreateObject("VBScript.RegExp")

    Reg1.Global = True
    Reg1.Pattern = "([\d][\d][\d][\d][\d][\d])"
    If Reg1.Test(Item.Subject) Then
        Set MailDest = ns.Folders("Inbox").Folders("AMEX")
        Item.Move MailDest
    End If
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Reidacus
  • 103
  • 2
  • 13
  • the regex seems fine, do you get any errors? or its just not moving the emails to the desired folder? – Sol Stein Aug 25 '16 at 19:03
  • There are no errors thrown at all but the mail just stays in the inbox when i rerun the rule that executes the script and moves no where. – Reidacus Aug 25 '16 at 19:12
  • the rule is based on new emails coming in? or existing emails? did you try to send an email to that account with 6 digits in the subject? – Sol Stein Aug 25 '16 at 19:18
  • Yes, the rule should be based on new mails coming in. I also have tried to send an email to that account with 6 digits in the subject and it just comes through and sits in the inbox still. No errors thrown. – Reidacus Aug 25 '16 at 19:30
  • Does the code actually run when a new mail comes in? – Tim Williams Aug 25 '16 at 19:37
  • ok, so now lets rule out one more thing, have you setup the rule properly? Create rule, on select action choose run a script > click on the script select the script choose ok make sure turn on this rule is checked. – Sol Stein Aug 25 '16 at 19:38
  • There is a rule which is set to trigger the script. Aside from that, how can I tell if it attempted to run or not? – Reidacus Aug 25 '16 at 19:38
  • add a snippet on the first line MsgBox "Hi just got into the script" and send a new email with 6 digits look if you get the msgbox. – Sol Stein Aug 25 '16 at 19:40
  • Yes I get the Msgbox text displaying when the rule is run so at least we know it is talking to the script. – Reidacus Aug 25 '16 at 19:43
  • Looks like your Regex is set to detect a Subject Line that ONLY contains six consecutive digits. To detect six consecutive digits within a longer Subject Line, use the Regex code "\b\d{6}\b". Hope this helps! – tbur Aug 25 '16 at 19:50
  • I tried your reg ex but that does nto work either im afriad. Same results as last time. Msgbox is being displayed but the email does not move folder – Reidacus Aug 25 '16 at 19:58
  • ok, i think i got the problem, how many accounts do you have in your outlook? – Sol Stein Aug 25 '16 at 20:00
  • Just the one single one :) The account is on an Exchange server so not sure if that may make any difference? – Reidacus Aug 25 '16 at 20:06

2 Answers2

4

your problem is with the folder name replace Set MailDest = ns.Folders("Inbox").Folders("AMEX")

with this line

Set MailDest = ns.Folders("enteryouraccountname@yourhost.com").Folders("Inbox").Folders("AMEX")

and dont forget to put in your account name

Sol Stein
  • 656
  • 10
  • 29
3

You could also set it like this.

Set MailDest = ns.GetDefaultFolder(olFolderInbox).Folders("AMEX")

GetDefaultFolder Method

0m3r
  • 12,286
  • 15
  • 35
  • 71