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

kco*_*ock 48 regex vbscript vba outlook-vba outlook-2007

是否可以基于正则表达式字符串在Outlook 2007中创建规则?

我想添加过滤器包含一个字符串,如消息:4000-10,四位数字后加破折号后两位数,这可以从任何东西0000-009999-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
Run Code Online (Sandbox Code Playgroud)

Chr*_*dge 51

我不知道是否可以在规则中直接使用正则表达式,但是您可以使用规则触发脚本,并且脚本可以使用正则表达式.我讨厌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
Run Code Online (Sandbox Code Playgroud)

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

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

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

Apply this rule after the message arrives
on this machine only
run a script
Run Code Online (Sandbox Code Playgroud)

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

您可以单击"下一步"以向规则添加例外,或者如果没有例外,则单击"完成".

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

如果您还没有代码签名密钥,可以使用OpenSSL 创建密钥.

我提到过我讨厌Outlook吗?


Eth*_*her 17

Microsoft Outlook不支持正则表达式.您可以执行通配符搜索,但由于某些莫名其妙的原因,通配符%不是*.

  • 这篇知识库文章不是关于规则向导本身,而是关于CRM的附加组件.如果你解析这篇文章,你会发现在这种情况下使用%符号的原因是完全合理的.这是因为它是一个SQL查询,其中通配符确实是% (2认同)