The boss wants a button in Outlook to add a sender to a rule. I am testing this on my PC, so in my example I have a rule called "Photography". That rule moves emails (upon receipt) to a folder called "Stuff". There are a handful of email addresses already in this rule, so that email from "news@OnOne.com" will be put into the "Stuff" folder. The rule works fine.
Now, I need to be able to highlight an email in the inbox, click a button, and have the sender's address added to the list of senders in the rule. So, if I highlight an email from "email@example.com" and click my button, he gets added to the rule.
I'm pretty close, I think, with close I've cobbled together from other solutions.
It seems to add the address to the rule, but I get an error when I try to .Save the rule. See attached image.
Dim bFound As Boolean
bFound = False
Dim colRules As Outlook.Rules
Dim oRule As Outlook.Rule
Set colRules = Application.Session.DefaultStore.GetRules()
'--- find the rule object ...
For Each ruleItem In colRules
If ruleItem.Name = "Photography" Then
Set oRule = ruleItem
bFound = True
If bFound = False Then
MsgBox "Rule 'Photography' not found!"
'--- get selected item email address
' Dim Session As Outlook.NameSpace
Dim currentExplorer As Explorer
Dim Selection As Selection
Dim currentItem As Object
Dim currentMail As MailItem
Dim currentSenderAddress As String
Set currentExplorer = Application.ActiveExplorer
Set Selection = currentExplorer.Selection
'--- for all items do...
For Each currentItem In Selection
If currentItem.Class = olMail Then
Set currentMail = currentItem
currentSenderAddress = currentMail.SenderEmailAddress
'-- add it ...
Set oFromCondition = oRule.Conditions.From
'-- Update the server and display progress dialog