Outlook 2007 中的正则表达式规则? [英] Regular Expression Rules in Outlook 2007?

查看:53
本文介绍了Outlook 2007 中的正则表达式规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to create rules in Outlook 2007 based on a regex string?

我正在尝试为包含以下字符串的消息添加过滤器:4000-10,一个四位数字,后跟一个破折号,然后是一个两位数字,可以是以下任何内容0000-009999-99.

I'm trying to add a filter for messages containing a string such as: 4000-10, a four digit number followed by a dash and then a two digit number, which can be anything from 0000-00 to 9999-99.

我将其用作正则表达式:\b[0-9]{4}\-[0-9]{2}\b 但过滤器不起作用.我也尝试了一些其他修改,但没有运气.不过,我无法在网上找到任何关于 Outlook 是否支持将正则表达式输入规则的具体信息,所以我想我会在这里问,以防我浪费时间.

I was using this as a regex: \b[0-9]{4}\-[0-9]{2}\b but the filter isn't working. I've tried a few other modifications as well with no luck. I wasn't able to find anything concrete online about whether Outlook even supports entering regexes into a rule, though, so I figured I would ask here in case I'm wasting my time.

感谢下面克里斯的评论,我能够通过宏实现这个过滤器.我想我会在下面分享我的代码,以防它能够帮助其他人:

Thanks to Chris's comment below, I was able to implement this filter via a macro. I thought I would share my code below in case it is able to help anyone else:

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

推荐答案

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

I do not know if a regex can be used directly in a rule, but you can have a rule trigger a script and the script can use regexes. I hate Outlook.

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

First, you have to open the script editor via Tools - Macro - Open Visual Basic Editor (Alt-F11 is the shortcut).

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

The editor will open. It should contain a project outline in a small panel in the top-left corner. The project will be listed as VBAProject.OTM. Expand this item to reveal Microsoft Office Outlook Objects. Expand that to reveal ThisOutlookSession. Double-click ThisOutlookSession to open the code editing pane (which will probably be blank).

接下来选择工具菜单 |引用并启用称为Microsoft VBScript 正则表达式 5.5"之类的 RegExp 引用

Next select Tools menu | References and enable the RegExp references called something like "Microsoft VBScript Regular Expressions 5.5"

您现在可以创建一个子程序来执行您的过滤操作.请注意,规则调用的子例程必须具有 Outlook.MailItem 类型的单个参数.例如:

You can now create a subroutine to perform your filtering action. Note that a subroutine called by a rule must have a single parameter of type Outlook.MailItem. For example:

' 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

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

Next, you have to create a rule (Tools - Rules and Alerts) to trigger this script. Click the New Rule button on the dialog to launch the wizard. Select a template for the rule. Choose the "Check messages when they arrive" template from the "Start from a blank rule" category. Click Next.

选择仅在这台机器上"条件(不是很直观吗?),然后点击下一步.

Choose the "On this machine only" condition (intuitive isn't it?) and click next.

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

Choose the "run a script" option. At the bottom of the wizard where it shows your new rule, it should read:

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

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

The phrase "a script" is a clickable link. Click it and Outlook will display a dialog that should list the subroutine you created earlier. Select your subroutine and click the OK button.

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

You can click Next to add exceptions to the rule or click Finish if you have no exceptions.

现在,好像该过程还不够复杂,除非您使用代码签名密钥对脚本进行签名,否则每次停止和重新启动 Outlook 时,此规则都会停用.

Now, as though that process was not convoluted enough, this rule will deactivate every time you stop and restart Outlook unless you sign the script with a code signing key.

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

If you don't already have a code signing key, you can create one with OpenSSL.

我有没有提到我讨厌 Outlook?

Did I mention that I hate Outlook?

这篇关于Outlook 2007 中的正则表达式规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆