Outlook 2007中的正则表达式规则?

时间:2010-10-05 16:02:44

标签: regex vba vbscript outlook-vba outlook-2007

是否可以基于正则表达式字符串在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

2 个答案:

答案 0 :(得分:49)

我不知道是否可以在规则中直接使用正则表达式,但是您可以让规则触发脚本,并且脚本可以使用正则表达式。我讨厌Outlook。

首先,您必须通过工具 - 宏 - 打开Visual Basic编辑器打开脚本编辑器(Alt-F11是快捷方式)。

编辑将打开。它应该在左上角的小面板中包含项目轮廓。该项目将列为VBAProject.OTM。展开此项以显示Microsoft Office Outlook对象。展开它以显示ThisOutlookSession。双击ThisOutlookSession以打开代码编辑窗格(可能为空白)。

接下来选择工具菜单|引用并启用RegExp引用,称为“Microsoft VBScript Regular Expressions 5.5”

您现在可以创建子例程来执行过滤操作。请注意,规则调用的子例程必须具有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

接下来,您必须创建一个规则(工具 - 规则和警报)来触发此脚本。单击对话框上的“新建规则”按钮以启动向导。选择规则的模板。从“从空白规则开始”类别中选择“在到达时检查邮件”模板。单击“下一步”。

选择“仅在此机器上”条件(直观不是吗?)然后单击“下一步”。

选择“运行脚本”选项。在向导的底部显示新规则,它应显示为:

Apply this rule after the message arrives
on this machine only
run a script

短语“脚本”是可点击的链接。单击它,Outlook将显示一个对话框,该对话框应列出您之前创建的子例程。选择子例程,然后单击“确定”按钮。

您可以单击“下一步”向规则添加例外,如果没有例外,则单击“完成”。

现在,好像该过程不够复杂,除非您使用代码签名密钥对脚本进行签名,否则每次停止并重新启动Outlook时此规则都将取消激活。

如果您还没有代码签名密钥,则create one可以OpenSSL

我是否提到过我讨厌Outlook?

答案 1 :(得分:17)

Microsoft Outlook does not support regular expressions。您可以执行通配符搜索,但由于某些莫名其妙的原因,通配符为%,而不是*

相关问题