2
Our company uses a 6-digit number for our projects. I'm attempting to create a script that will search the email subject for this 6-digit number, then find the subfolder that starts with this 6-digit number, and move the email to that folder.
In my searches for how to do this I've stolen some code and come up with the below. I put in some MsgBox commands in an attempt to figure out if things are working along the way. However when I run the code (Rules>Manage Rules and Alerts>Run Rules Now) I don't get an error and I don't get any MsgBox's. Anyone have any thoughts/comments on what I might be doing wrong?
Sub filterbyprojectnumber(Item As Outlook.MailItem)
Dim nsOutlook As Outlook.NameSpace
Dim MailDest As Outlook.Folder
Set nsOutlook = Application.GetNamespace("MAPI")
Set RegExp = CreateObject("VBScript.RegExp")
MsgBox Item.Subject
RegExp.Global = True
RegExp.Pattern = "([^\d]|^)\d{6}([^\d]|$)"
If RegExp.Test(Item.Subject) Then
MsgBox Item.Subject
MailDest = FindInFolders(Application.Session.Folders, RegExp.Test(Item.Subject))
MsgBox MailDest
Item.Move MailDest
End If
End Sub
Function FindInFolders(TheFolders As Outlook.Folder, Name As String)
Dim SubFolder As Outlook.MAPIFolder
On Error Resume Next
Set FindInFolders = Nothing
For Each SubFolder In TheFolders
If LCase(SubFolder.Name) Like LCase(Name) Then
Set FindInFolders = SubFolder
Exit For
Else
Set FindInFolders = FindInFolders(SubFolder.Folders, Name)
If Not FindInFolders Is Nothing Then Exit For
End If
Next
End Function
Is runascript allowed in your setup? https://superuser.com/questions/1246894/after-outlook-updated-the-mail-rules-run-script-option-is-missing?rq=1
– niton – 2019-08-08T20:16:33.337Yes, run a script is allowed. – tincanfury – 2019-08-09T19:51:11.473
If you have not already tried, temporarily remove all options, if any, from the rule except "run a script" so the code runs on all received mail. Send yourself mail with any subject to see if the msgbox appears. – niton – 2019-08-09T20:09:01.873