是否可以基于正则表达式字符串在Outlook 2007中创建规则?
我正在尝试为包含以下字符串的邮件添加一个过滤器:4000-10
,一个四位数字,后跟一个破折号,然后是一个两位数字,可以是从0000-00
到9999-99
的任何数字。
我将其用作正则表达式:\b[0-9]{4}\-[0-9]{2}\b
,但过滤器不起作用。我也尝试了一些其他的修改,但都没有成功。不过,我在网上找不到任何关于Outlook是否支持在规则中输入正则表达式的具体内容,所以我想我应该在这里问一下,以防我在浪费时间。
编辑:感谢Chris在下面的评论,我能够通过一个宏来实现这个过滤器。我想我会在下面分享我的代码,以防它能帮助其他任何人:
Sub JobNumberFilter(Message As Outlook.MailItem)
Dim MatchesSubject, MatchesBody
Dim RegEx As New RegExp
'e.g. 1000-10'
RegEx.Pattern = "([0-9]{4}-[0-9]{2})"
'Check for pattern in subject and body'
If (RegEx.Test(Message.Subject) Or RegEx.Test(Message.Body)) Then
Set MatchesSubject = RegEx.Execute(Message.Subject)
Set MatchesBody = RegEx.Execute(Message.Body)
If Not (MatchesSubject Is Nothing And MatchesBody Is Nothing) Then
'Assign "Job Number" category'
Message.Categories = "Job Number"
Message.Save
End If
End If
End Sub
发布于 2010-10-06 01:35:01
我不知道正则表达式是否可以直接在规则中使用,但您可以让规则触发脚本,并且脚本可以使用正则表达式。我讨厌Outlook。
首先,您必须通过工具-宏-打开Visual Basic编辑器(快捷键为Alt-F11)打开脚本编辑器。
编辑器将打开。它应该在左上角的一个小面板中包含一个项目大纲。该项目将作为VBAProject.OTM列出。展开该项可显示Microsoft Office Outlook对象。将其展开以显示ThisOutlookSession。双击ThisOutlookSession打开代码编辑窗格(可能为空)。
接下来,选择工具菜单|引用并启用名为"Microsoft VBScript Regular Expression 5.5“之类的RegExp引用。
现在,您可以创建子例程来执行过滤操作。请注意,规则调用的子例程必须有一个Outlook.MailItem类型的参数。例如:
' note that Stack Overflow's syntax highlighting doesn't understand VBScript's
' comment character (the single quote) - it treats it as a string delimiter. To
' make the code appear correctly, each comment must be closed with another single
' quote so that the syntax highlighter will stop coloring everything as a string.'
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
RegEx.IgnoreCase = True
' assume mail is good'
Dim Message As String: Message = ""
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)"
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, ",")
End If
End If
' other tests'
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
' similar to above, but grab Deleted Items folder as destination of move'
Case Actions.ACT_DELIVER
' do nothing'
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
接下来,您必须创建一个规则(Tools - Rules and Alerts)来触发此脚本。单击对话框上的New Rule按钮以启动向导。为规则选择模板。从“从空白规则开始”类别中选择“消息到达时检查”模板。单击下一步。
选择"On this machine only“条件(直观,不是吗?)然后单击下一步。
选择"run a script“选项。在显示新规则的向导底部,应为:
Apply this rule after the message arrives
on this machine only
run a script
短语“脚本”是一个可点击的链接。单击它,Outlook将显示一个对话框,其中应列出您先前创建的子例程。选择子例程,然后单击OK按钮。
您可以单击“下一步”将例外添加到规则中,如果没有例外,也可以单击“完成”。
现在,就好像这个过程还不够复杂一样,除非您使用代码签名密钥对脚本进行签名,否则每次您停止并重新启动Outlook时,此规则都将停用。
如果您还没有代码签名密钥,您可以使用OpenSSL进行create one。
我有没有提到过我讨厌Outlook?
发布于 2010-10-06 00:33:24
Microsoft Outlook does not support regular expressions。您可以执行通配符搜索,尽管出于某种无法解释的原因,通配符是%
而不是*
。
https://stackoverflow.com/questions/3865500
复制相似问题