-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRules.bas
156 lines (110 loc) · 4 KB
/
Rules.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Attribute VB_Name = "Rules"
'Usage: add to a rule using "Run a script"
'Revisions:
' 2019/01/14 - Craig Buchanan - creating code
'Notes:
' enable 'run a script' option: https://www.extendoffice.com/documents/outlook/4640-outlook-rule-run-a-script-missing.html
'
' controller for processing an Inbox message that may contain a RITM
'
Sub ProcessMailItem(Item As Outlook.MailItem)
On Error GoTo ProcessMailItem_err
Dim ticketNumber As String
ticketNumber = ExtractTicketID(Item)
If ticketNumber <> "" Then
' if there is NOT a folder that matches the RIMT, create one
Dim Target As Outlook.MAPIFolder
Set Target = Find_or_Create_Folder(ticketNumber)
' move message to folder
Item.Move Target
End If
ProcessMailItem_exit:
Set Target = Nothing
On Error GoTo 0
Exit Sub
ProcessMailItem_err:
Select Case Err.Number
Case Else
MsgBox Err.Description & " [" & Err.Number & "]", vbExclamation, "Error in ProcessMailItem"
End Select
Resume ProcessMailItem_exit
End Sub
'
' find a ticket number (matches pattern RITM9999999) embedded in message's subject or body
'
Function ExtractTicketID(Item As Outlook.MailItem) As String
' pattern
Dim ticketPattern As String
' \d Match any digit
' {} Specifies how many times a token can be repeated
' $ Match must occur at the end of the string
ticketPattern = "RITM\d{7}"
Dim RegExp As RegExp: Set RegExp = New RegExp
RegExp.Pattern = ticketPattern
Dim Matches As MatchCollection
If RegExp.Test(Item.Subject) Then
' extract RITM
Set Matches = RegExp.Execute(Item.Subject)
ExtractTicketID = Matches(0).Value
ElseIf RegExp.Test(Item.Body) Then
' extract RITM
Set Matches = RegExp.Execute(Item.Body)
ExtractTicketID = Matches(0).Value
Else
ExtractTicketID = vbNullString
End If
End Function
'
' locate a folder by its name; if it doesn't exist, create it as immediate child of Inbox node
'
Function Find_or_Create_Folder(FolderName As String)
' search Inbox and its children
Dim Inbox As Outlook.MAPIFolder
Set Inbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
'
Dim Folder As Outlook.MAPIFolder
Set Folder = FindInFolders(Inbox.Folders, FolderName)
If Folder Is Nothing Then
Set Folder = Inbox.Folders.Add(FolderName, olFolderInbox)
End If
Set Find_or_Create_Folder = Folder
Find_or_Create_Folder_exit:
Set Folder = Nothing
Set Inbox = Nothing
Exit Function
Find_or_Create_Folder_err:
Select Case Err.Number
Case Else
MsgBox Err.Description, vbExclamation, "Error"
End Select
Resume Find_or_Create_Folder_exit
End Function
'
' Add a folder to the "Favorties" section of Outlook
'
Function AddToFavorites(Favorite As MAPIFolder)
Dim MailModule As Outlook.MailModule
Set MailModule = Application.ActiveExplorer().NavigationPane.Modules.GetNavigationModule(Outlook.OlNavigationModuleType.olModuleMail)
Dim FavoriteGroup As Outlook.NavigationGroup
Set FavoriteGroup = MailModule.NavigationGroups.GetDefaultNavigationGroup(Outlook.OlGroupType.olFavoriteFoldersGroup)
Set AddToFavorites = FavoriteGroup.NavigationFolders.Add(Favorite)
Set FavoriteGroup = Nothing
Set MailModule = Nothing
End Function
'
' Purpose: recurse folder collection to find a folder
'
Private Function FindInFolders(TheFolders As Outlook.Folders, 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