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

I am trying to create a custom rule for Outlook 2010 that checks an email subject, and if it makes a regular expression, it moves to a specific folder.

However, when I run the script, I get the following error when trying to get the Outlook.Folder object for the folder where I want to move the message:

Runtime Error '91':
Object variable or with block variable not set

Below is the VBA script that I use 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 
+6
source share
2 answers

Your last but one line should be

 Set GetFolder = ObjFolder 
+4
source

In your GetFolder function GetFolder you also hard-coded the folder name.

The line reads:

 Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders("ETS") 

Must read:

 Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders(FolderName) 
0
source

Source: https://habr.com/ru/post/885467/


All Articles