Bruce Johnson
Member
- Outlook version
- Outlook 2013 32 bit
- Email Account
- POP3
Greetings all,
I have found this "CreateRuleForBlockingMultipleSenders" code that creates a rule for selected email.
However, each time I run it, ire cteates a NEW RULE with the same name, and a new set of sender to block...
What I would LIKE to to is to have the code MODIFY the CURRENT rule and then ADD the selected senders to that rule.
Here is the code (source was How to Quickly Create an Outlook Rule to Block the Senders of Multiple Emails )
Thanks in advance!
Mc
I have found this "CreateRuleForBlockingMultipleSenders" code that creates a rule for selected email.
However, each time I run it, ire cteates a NEW RULE with the same name, and a new set of sender to block...
What I would LIKE to to is to have the code MODIFY the CURRENT rule and then ADD the selected senders to that rule.
Here is the code (source was How to Quickly Create an Outlook Rule to Block the Senders of Multiple Emails )
Code:
Sub CreateRuleForBlockingMultipleSenders()
Dim objRules As Outlook.Rules
Dim objRule As Outlook.Rule
Dim objMoveRuleAction As Outlook.MoveOrCopyRuleAction
Dim objFromCondition As Outlook.ToOrFromRuleCondition
Dim objSelection As Outlook.Selection
Dim objMail As Outlook.mailItem
Dim objJunkFolder As Outlook.Folder
Set objRules = Application.Session.DefaultStore.GetRules()
'Create a rule
Set objRule = objRules.Create("Block Senders", olRuleReceive)
Set objFromCondition = objRule.Conditions.From
'Get the selected emails
Set objSelection = Outlook.Application.ActiveExplorer.Selection
'Get Junk Email folder
Set objJunkFolder = Outlook.Application.Session.GetDefaultFolder(olFolderJunk)
For i = objSelection.Count To 1 Step -1
If objSelection.Item(i).Class = olMail Then
Set objMail = objSelection.Item(i)
'Add the email senders to rule condition
With objFromCondition
.Enabled = True
.Recipients.Add objMail.SenderEmailAddress
.Recipients.ResolveAll
End With
'Move the emails from blocked senders to Junk Email folder
Set objMoveRuleAction = objRule.Actions.MoveToFolder
With objMoveRuleAction
.Enabled = True
.Folder = objJunkFolder
End With
End If
Next
objRules.Save
End Sub
Thanks in advance!
Mc