我正在使用Outlook 2010的VBA宏,以将传入的电子邮件过滤和分类到不同的文件夹中。目标中提到了规则在实施和测试时,它会提示错误消息框,而不是成功进行过滤。您能告诉我默认通话Application_NewMail
下的哪一部分继续进行?
目标:
在[此括号]中提取单词
主题:
[ABC]
->创建收件箱文件夹ABC
编程语言:VBA宏
Outlook版本:2010
错误消息:
User-defined Type not defined
错误部分:
Sub MyNiftyFilter(Item As Outlook.MailItem)
Dim Matches, Match
Dim regex As New RegExp
Dim mc As System.Text.RegularExpressions.MatchCollection <-----
完整代码:
Public Enum Actions
ACT_DELIVER = 0
ACT_DELETE = 1
ACT_QUARANTINE = 2
End Enum
Sub MyNiftyFilter(Item As Outlook.MailItem)
Dim Matches, Match
Dim regex As New RegExp
Dim mc As System.Text.RegularExpressions.MatchCollection
regex.IgnoreCase = True
Dim GoodRegEx As New RegExp
GoodRegEx.IgnoreCase = True
' assume mail is good'
Dim Message As String: Message = ""
Dim GroupName As String: GroupName = ""
Dim Action As Actions: Action = ACT_DELIVER
' SPAM TEST: Illegal word in subject'
regex.Pattern = "(v\|agra|erection|penis|boner|pharmacy|painkiller|vicodin|valium|adderol|sex med|pills|pilules|viagra|cialis|levitra|rolex|diploma)"
GoodRegEx.Pattern = "(([\w-\s]*)\s*)"
If Action = ACT_DELIVER Then
If regex.test(Item.Subject) Then
Action = ACT_QUARANTINE
Set Matches = regex.Execute(Item.Subject)
Message = "SPAM: Subject contains restricted word(s): " & JoinMatches(Matches, ",")
ElseIf GoodRegEx.test(Item.Subject) Then
Dim results(mc.Count - 1) As String
For i = 0 To results.Length - 1
results(i) = mc(i).Value
If i = 0 Then
GroupName = results(i)
Set MailDest = ns.Folders(GroupName)
Item.Move MailDest
End If
Next
End If
End If
Select Case Action
Case Actions.ACT_QUARANTINE
Dim ns As Outlook.NameSpace
Set ns = Application.GetNamespace("MAPI")
Dim junk As Outlook.Folder
Set junk = ns.GetDefaultFolder(olFolderJunk)
Item.Subject = "SPAM: " & Item.Subject
If Item.BodyFormat = olFormatHTML Then
Item.HTMLBody = "<h2>" & Message & "</h2>" & Item.HTMLBody
Else
Item.Body = Message & vbCrLf & vbCrLf & Item.Body
End If
Item.Save
Item.Move junk
Case Actions.ACT_DELETE
Case Actions.ACT_DELIVER
End Select
End Sub
Private Function JoinMatches(Matches, Delimeter)
Dim RVal: RVal = ""
For Each Match In Matches
If Len(RVal) <> 0 Then
RVal = RVal & ", " & Match.Value
Else
RVal = RVal & Match.Value
End If
Next
JoinMatches = RVal
End Function
Private Sub Application_NewMail()
Dim Item As Outlook.MailItem
MyNiftyFilter Item
End Sub
您可以使用ItemAdd event
https://stackoverflow.com/a/58428753/4539709或将NewMail
固定为简单的
Private Sub Application_NewMail()
Dim Item As Outlook.MailItem
MyNiftyFilter Item
End Sub
当新邮件到达收件箱时以及在进行客户端规则处理之前,将触发NewMail事件。如果要处理到达收件箱的项目,请考虑对收件箱中的项目集合使用ItemAdd事件。 ItemAdd事件传递对添加到文件夹的每个项目的引用。