Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outlook 2010 custom VBA script to move incoming mail message to a specific folder

I'm trying to create a custom rule for Outlook 2010 that inspect the subject of the email and if it makes a regular expression it's moved into a specific folder.

However when I run the script I get the following error when I try and get an Outlook.Folder object for the folder I want to move the message to:

Run-time error '91':
Object variable or With block variable not set

Below is the VBA script that I am using to check the email subject and move the message to the specified folder if it matches.

Sub MoveToETS(Item As Outlook.MailItem)
    Dim Subject As String
    Subject = Item.Subject

    Dim FolderToMoveTo As Outlook.Folder
    Set FolderToMoveTo = GetFolder("ETS")

    If (CheckSubject(Subject, "^[Project|Bug] (\d+?) - \[[UPDATE|NEW|RESOLVED]\]")) Then
        Item.Move (FolderToMoveTo)
    End If
End Sub

Function CheckSubject(Subject As String, PatternToCheck As String)
    Dim ObjRegExp As RegExp
    Dim ObjMatch As Match

    Set ObjRegExp = New RegExp
    ObjRegExp.Pattern = PatternToCheck

    If (ObjRegExp.Text(Subject) = True) Then
        CheckSubject = True
    End If

End Function

Function GetFolder(ByVal FolderName As String) As Outlook.Folder

    Dim ObjFolder As Outlook.Folder

    Set ObjFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders("ETS")

    GetFolder = ObjFolder

End Function
like image 489
noxee Avatar asked Apr 08 '11 02:04

noxee


People also ask

How do I make Outlook move emails received to a specific folder?

To quickly create a rule that moves all email from a specific sender or a set of senders to a folder, right-click a message in your message list that you want to create a rule for, and select Create rule. Choose the folder where you want all messages from that sender or set of senders to be moved, and then select OK.

How do I automatically move emails from inbox to personal folder in Outlook 2010?

With a message selected and previewed in the Reading Pane or open in its own window, in the Move group, click Rules, and then click Always Move Messages From. Select a folder, and then click OK.


1 Answers

Your last but one line needs to be

Set GetFolder = ObjFolder
like image 185
paulmorriss Avatar answered Sep 22 '22 01:09

paulmorriss