I have used the many helpful threads in this forum and almost have my script working.
I receive an automated email from a system and want to grab all emails from a specific domain listed within the message body and forward the message to these addresses. The code below works but if I find 3 address matches it send 3 emails. If I use the second code listed I can get all the addresses in a string separated by semi-colons. But it does not like adding the string to the recipients list.
Code to try and send to all discovered addresses in one email.
I have tried code below but not sure I have it just right.
For i = 0 To UBound(Split(strAlias, ";"))
Set ToForward = MyForward.Recipients.Add(Split(strAlias, ";")(i))
ToForward.Type = ItemTo
Next i
Thanks
I receive an automated email from a system and want to grab all emails from a specific domain listed within the message body and forward the message to these addresses. The code below works but if I find 3 address matches it send 3 emails. If I use the second code listed I can get all the addresses in a string separated by semi-colons. But it does not like adding the string to the recipients list.
PHP:
Sub First(Item As Outlook.MailItem)
Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Set Reg1 = New RegExp
Dim strAlias As String
With Reg1
'.Pattern = "([a-z0-9.]*)@([a-z0-9.]*)"
.Pattern = "([a-z0-9.]*)@candu.com"
.Global = True
End With
If Reg1.test(Item.Body) Then
Set M1 = Reg1.Execute(Item.Body)
For Each M In M1
strAlias = M '& ";" & strAlias
'MsgBox "reg1: " & strAlias
'Item.Subject = "Test"
Set MyForward = Item.Forward
strSubj = Replace(MyForward.Subject, "FW: ", "")
MyForward.Subject = strSubj
MyForward.Recipients.Add strAlias
MyForward.Send
'Item.Save
Next
End If
'MsgBox "reg2: " & strAlias
End Sub
Code to try and send to all discovered addresses in one email.
PHP:
Sub First(Item As Outlook.MailItem)
Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Set Reg1 = New RegExp
Dim strAlias As String
With Reg1
'.Pattern = "([a-z0-9.]*)@([a-z0-9.]*)"
.Pattern = "([a-z0-9.]*)@mydomain.com"
.Global = True
End With
If Reg1.test(Item.Body) Then
Set M1 = Reg1.Execute(Item.Body)
For Each M In M1
strAlias = M & ";" & strAlias
'MsgBox "reg1: " & strAlias
'Item.Subject = "Test"
'Item.Save
Next
End If
MsgBox "reg1: " & strAlias
Set MyForward = Item.Forward
strSubj = Replace(MyForward.Subject, "FW: ", "")
MyForward.Subject = strSubj
MyForward.Recipients.Add strAlias
MyForward.Send
End Sub
I have tried code below but not sure I have it just right.
For i = 0 To UBound(Split(strAlias, ";"))
Set ToForward = MyForward.Recipients.Add(Split(strAlias, ";")(i))
ToForward.Type = ItemTo
Next i
Thanks