我收到了大量来自我们的票务系统发来的垃圾邮件,大多数我都忽略了,但偶尔有人会提到“西蒙,你能看看这个吗?”
如果邮件中包含“simon”这个词,我想将其过滤到子文件夹中,但不幸的是,每封邮件的结尾都是这样的
“已向用户 John Smith 和 Simon Johnson 发送消息”
因此,如果我添加一条规则来检测“Simon”,它会将每封电子邮件移动到该文件夹中。
如果我排除“西蒙·约翰逊”,那么就不会移动任何电子邮件。
有没有办法让它需要 >1 个 Simon 实例,或者只匹配 Simon 而忽略 Simon Johnson?
答案1
VBA
如下脚本可以完成这个工作:
Option Explicit
Sub DoubleSimonMessageRule(newMail As Outlook.mailItem)
Dim a() As String ' we convert the mail body to an array of string
Dim EntryID As String
Dim StoreID As Variant
Dim mi As Outlook.mailItem
Dim dest As String
Dim destFldr As Outlook.Folder
Dim I As Integer
Dim iMatch As Integer
Const pattern = "Simon"
Const dest1 = "Simon1" ' destination folder for 1 match
Const destAny = "SimonAny" ' destination folder for 2+ matches
On Error GoTo ErrHandler
' we have to access the new mail via an application reference
' to avoid security warnings
EntryID = newMail.EntryID
StoreID = newMail.Parent.StoreID
Set mi = Application.Session.GetItemFromID(EntryID, StoreID)
a = Split(mi.body, vbCrLf)
iMatch = 0
For I = LCase(a) To UCase(a)
If InStr(1, a(I), pattern, vbTextCompare) Then
iMatch = iMatch + 1
End If
Next I
If iMatch < 1 Then
' this should not happen, provided our rule is configured properly
Err.Raise 1, , "No " & pattern & " in Mail"
ElseIf iMatch = 1 Then
dest = dest1
ElseIf iMatch > 1 Then
dest = destAny
End If
Set destFldr = Application.GetNamespace("MAPI").Folders(dest)
mi.Move destFldr
' mi.Delete ' not sure about this!
Set mi = Nothing
Exit Sub
ErrHandler:
Debug.Print Err.Description
Err.Clear
On Error GoTo 0
End Sub
使用 Outlook 规则助手对邮件正文中包含“Simon”的传入邮件调用此脚本。