Outlook 转发和正则表达式 [英] Outlook Forwarding and Regular Expressions
问题描述
我正在尝试在 Outlook 中设置一个规则,每当我收到具有特定主题的电子邮件时,脚本都会运行并解析该电子邮件正文并返回一个长度为 4-6 个字符的数字.例如 4444 或 123456.
I am trying to set a rule within Outlook that whenever I receive an email with a certain subject a script runs and parses through that emails body and returns a number that ranges from 4-6 characters long. For example 4444 or 123456.
收到的每封电子邮件仅包含这些数字中的一个,长度范围为 4 到 6 个字符,以及电子邮件中的其他信息.我希望返回的 4 到 6 位数字包含在发送到不同地址的新电子邮件的电子邮件主题中.
Each email coming in contains only one of these numbers that ranges from 4 to 6 characters long, along with other information within the email. I want that 4 to 6 digit number returned to be in the email subject of a new email message being sent to a different address.
这是我写的剧本.
Sub Forward(Item As Outlook.MailItem)
Dim M1 As MatchCollection
Dim M As Match
Set Reg1 = New RegExp
With Reg1
.Pattern = "([0-9]{4-6})"
.Global = True
End With
If Reg1.Test(Item.Body) Then
Set M1 = Reg1.Execute(Item.Body)
For Each M In M1
'allows for multiple matches in the message body
Item.Subject = M.SubMatches(1) & "; " & Item.Subject
Next
End If
Item.Save
Set myForward = Item.Forward
myForward.Recipients.Add "xxxx@yahoo.com"
myForward.Send
End Sub
我了解如何为每封带有特定主题的电子邮件创建触发规则,但我是 VBA 新手,在完成这项简单任务时遇到了麻烦.我在测试"上遇到一个对象错误,我不知道如何解决这个问题.
I understand on how to create a rule to trigger for each email coming in with a specific subject, but I'm new to VBA and am having trouble with this simple task. I'm getting an object Error on "Test" and I'm not sure how to fix this problem.
推荐答案
你差不多明白了,看下面的例子...
You almost got it, see example below...
Option Explicit
Public Sub Forward(Item As Outlook.MailItem)
Dim M1 As MatchCollection
Dim M As Match
Dim Reg1 As Object
Dim myForward As Object
Set Reg1 = New RegExp
With Reg1
.Pattern = "([0-9]{4,6})"
.Global = True
End With
If Reg1.Test(Item.Body) Then
Set M1 = Reg1.Execute(Item.Body)
For Each M In M1
Debug.Print M.SubMatches(0) ' Immediate Window
'// allows for multiple matches in the message body
Item.Subject = M.SubMatches(0) & "; " & Item.Subject
Next
End If
Item.Save
Set myForward = Item.Forward
myForward.Recipients.Add "xxxx@yahoo.com"
myForward.Display
End Sub
这篇关于Outlook 转发和正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!