Reidacus
Reidacus

Reputation: 103

Move incoming email to folders with RegEx in a rule

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

Upvotes: 0

Views: 1881

Answers (2)

0m3r
0m3r

Reputation: 12499

You could also set it like this.

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

GetDefaultFolder Method

Upvotes: 3

Sol Stein
Sol Stein

Reputation: 656

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

with this line

Set MailDest = ns.Folders("[email protected]").Folders("Inbox").Folders("AMEX")

and dont forget to put in your account name

Upvotes: 4

Related Questions