Skip to main content
 首页 » 编程设计

regex之Outlook 2007 中的正则表达式规则

2024年05月22日47飞鱼

是否可以在 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 

请您参考如下方法:

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

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

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

接下来选择“工具”菜单 |引用并启用名为“Microsoft VBScript Regular Expressions 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 

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

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

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

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

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

您可以单击“下一步”向规则添加异常(exception),或者如果没有异常(exception),请单击“完成”。

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

如果您还没有代码签名 key ,则可以 create oneOpenSSL .

我有没有提到过我讨厌 Outlook?