Modify VBA to create a RULE to block multiple messages

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 )
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
 
Similar threads
Thread starter Title Forum Replies Date
O modify vba to run it as script rule Outlook VBA and Custom Forms 8
S VBA to modify appointment item in additional Exchange account doesn't work Using Outlook 0
S Macro to extract and modify links from emails Outlook VBA and Custom Forms 3
R Outlook 2010 Modify Style "Do not check spelling or grammar" not saving Outlook VBA and Custom Forms 0
Justo Horrillo It's possible to modify the behaviour of the conversation option? Using Outlook 2
S Modify account template? BCM (Business Contact Manager) 0
R Can't modify Outlook view font with IE anymore (even though IE still affects print font) Using Outlook 5
smokiibear How to modify Today Page to Include tasks from other task folders Using Outlook 1
S Modify recurring outlook invites based on form entry Using Outlook 0
C How To Open Outlook Attachment in Excel, Modify some Data, then Re-Save It Using Outlook 3
A Possible to modify people preview window? Using Outlook 1
R How to modify Outlook Select Rooms form columns Using Outlook 1
G Modify Subject line of incoming mail where I am in Cc line Using Outlook 1
N modify New mesage SEND button Outlook VBA and Custom Forms 1
R Custom Archive code -- modify my code! Outlook VBA and Custom Forms 3
C How do I grab a field property (like .value) so I can modify it via OL code? Outlook VBA and Custom Forms 4
A Script to either modify "from" address or prevent a reply being se Outlook VBA and Custom Forms 2
B Can't modify PR_CLIENT_SUBMIT_TIME? Outlook VBA and Custom Forms 1
H using VBA to edit subject line Outlook VBA and Custom Forms 0
G Get current open draft message body from VBA Outlook VBA and Custom Forms 1
Geldner Problem submitting SPAM using Outlook VBA Form Outlook VBA and Custom Forms 2
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
M Outlook 2016 outlook vba to look into shared mailbox Outlook VBA and Custom Forms 0
V VBA Categories unrelated to visible calendar and Visual appointment Categories Outlook VBA and Custom Forms 2
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 3
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
R Outlook 2019 VBA to List Meetings in Rooms Outlook VBA and Custom Forms 0
geoffnoakes Counting and/or listing fired reminders via VBA Using Outlook 1
O VBA - Regex - remove double line spacing Outlook VBA and Custom Forms 1
D.Moore Strange VBA error Outlook VBA and Custom Forms 4
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 5
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
BartH VBA no longer working in Outlook Outlook VBA and Custom Forms 1
W Can vba(for outlook) do these 2 things or not? Outlook VBA and Custom Forms 2
MattC Changing the font of an email with VBA Outlook VBA and Custom Forms 1
P MailItem.To Property with VBA not work Outlook VBA and Custom Forms 2
P Tweak vba so it can target another mailbox Outlook VBA and Custom Forms 1
A Outlook 2010 VBA fails to launch Outlook VBA and Custom Forms 2
richardwing Outlook 365 VBA to access "Other Actions" menu for incoming emails in outlook Outlook VBA and Custom Forms 0
W Create a Quick Step or VBA to SAVE AS PDF in G:|Data|Client File Outlook VBA and Custom Forms 1
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
C Outlook (desktop app for Microsoft365) restarts every time I save my VBA? Using Outlook 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
TedSch Small vba to kill political email Outlook VBA and Custom Forms 3
E Outlook 365 Outlook/VBA Outlook VBA and Custom Forms 11
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Z VBA Forward vs manual forward Outlook VBA and Custom Forms 2
J VBA Cannot programmatically input or change Value for User Defined field Using Outlook 1

Similar threads

Back
Top