Link to home
Start Free TrialLog in
Avatar of Rob Rudloff
Rob RudloffFlag for United States of America

asked on

Adding a sender's address to an Outlook Rule via VB for Applications

Hi.
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 "bob@photo.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.

Any suggestions?
Thanks


Sub AddToRule_RETAIL()
    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
        Debug.Print ruleItem.Name
        If ruleItem.Name = "Photography" Then
            Set oRule = ruleItem
            bFound = True
            Exit For
       End If
    Next
    
    If bFound = False Then
        MsgBox "Rule 'Photography' not found!"
        Exit Sub
    End If
    
    '--- 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
            With oFromCondition
                 .Recipients.Add (currentSenderAddress)
            End With
            
            '-- Update the server and display progress dialog
            colRules.Save
        End If
    Next

End Sub

Open in new window

Capture.JPG
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rob Rudloff

ASKER

Thanks BDF:
That executes without error, hitting each line as expected.
However, the email address doesn't get added to the rule  -- I highlighted an email from tapaz.com and it did see it in the .SenderEmailAddress property, but you can see in the attached screen capture that only one sender is in the rule still.

I thought perhaps it had something to do with Exchange -- I noticed my test "Photography" rule is not "client-only", but I have a few rules that are.   I switched the code to use the "Jobs" rule, but the sender's email address didn't show up in there either.

Any ideas?
Thanks!
Capture.2JPG.JPG
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BlueDevilFan:
  I know there's a difference, but I don't know what it is
...  If I use "from people", does that mean that those people need to exist as a Contact in Outlook?   The rule should work for emails from everyone, so I reckon I want to use "specific words in the sender's address" ?

Thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK,  I'll change the rule to "from people" and try again.
Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Now I understand the terminology -- Yes, I have multiple mailboxes:  2 on different Exchange servers and another that is POP.
Just for my education, I will put a rule in the POP account (.pst) and see it generate the error.

After showing this to the boss, he suggests adding a combo box (which means adding a form) that will show him the rules, and let him select to which rule it will add the "sender email address".  I will post my attempt at that code later, and post a link to it here, perhaps.

Thanks for your help!