我正在尝试在Outlook中设置一个规则,每当我收到某个主题的电子邮件时,脚本就会运行并解析该电子邮件正文,并返回一个长度在4-6个字符之间的数字。例如4444或123456。
传入的每封电子邮件只包含其中一个数字,长度在4到6个字符之间,以及电子邮件中的其他信息。我希望返回的4到6位数字在电子邮件主题中的新电子邮件消息是发送到不同的地址。
这是我写的脚本。
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的新手,在这个简单的任务上遇到了麻烦。我在"Test“上得到了一个对象错误,我不确定如何解决这个问题。
发布于 2017-01-20 07:31:09
你几乎得到了它,请看下面的例子。
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 Subhttps://stackoverflow.com/questions/41752119
复制相似问题