Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook 2010 Rules Not Running Automatically - Using a VBA Fix

Status
Not open for further replies.

kjv1611

New member
Jul 9, 2003
10,758
US
For now, I've got a VBA fix in place and it works fine. However, I'd be keenly interested to know if anyone knows of a solution to the issue of the rules not running automatically.

I recently went through my Outlook rules and cleared out old names for folks who left the company, moved, whatever, and added a few that I didn't already have in my rules. I don't think I made any major changes.

During that time, I had a couple instances where I'd get the message stating that "one or more rules have been disabled" because I apparently (I guess) had one email address in more than one rule... or I think it also happens if someone leaves the company and their Outlook contact is now NULL, but I'm not 100% sure - it just seems to be the case on occasion.

So, it was driving me batty for about the past half a week to 1 full week. So this morning, I went looking around on the web. I didn't find an immediate solution, but I did cobble together a VBA solution.

First, I found some code that runs all rules for you whenever it is called. It's not complex code at all, but since it was already there, I just used that instead of typing myself:
Code:
Sub RunAllInboxRules()
'from [URL unfurl="true"]http://www.outlookcode.com/codedetail.aspx?id=1266[/URL]
    Dim st As Outlook.Store
    Dim myRules As Outlook.Rules
    Dim rl As Outlook.Rule
    Dim count As Integer
    Dim ruleList As String
    'On Error Resume Next
    
    ' get default store (where rules live)
    Set st = Application.Session.DefaultStore
    ' get rules
    Set myRules = st.GetRules
    
    ' iterate all the rules
    For Each rl In myRules
        ' determine if it's an Inbox rule
        If rl.RuleType = olRuleReceive Then
            ' if so, run it
            rl.Execute ShowProgress:=True
            count = count + 1
            ruleList = ruleList & vbCrLf & rl.Name
        End If
    Next
    
    ' tell the user what you did
    'ruleList = "These rules were executed against the Inbox: " & vbCrLf & ruleList
    'MsgBox ruleList, vbInformation, "Macro: RunAllInboxRules"
    
    Set rl = Nothing
    Set st = Nothing
    Set myRules = Nothing
End Sub
I commented out the MsgBox piece, b/c I don't want it popping up every time it runs.

And then I found another bit of code that someone put together for running anything on a set time. Well, that would work, but I don't want to be running something every few seconds if it's not necessary. So I thought of a better option.

I found the Application_NewMail() item in Outlook, and just called the RunAllInboxRules procedure from there. So that code looks like:
Code:
Private Sub Application_NewMail()
    RunAllInboxRules
End Sub

That last piece is in the ThisOutlookSession object, and the other is in its own standard module.

So this is working just fine. However, if anyone knows of a fix outside running via VBA, I'd like to know that and try it as well. I could not find any definite clues to it in my searching this morning.
 
Well, there has been one odd annoying thing - not really that bad, but just slightly annoying. So far, whenever a new email comes in, the rules run fine, but Outlook takes the focus from whatever else I'm doing.

However, I've commented out a couple lines, and changed one line from True to False, and I think that has fixed it.

So now this line:
rl.Execute ShowProgress:=[blue]True[/blue]

Has been changed to this:
rl.Execute ShowProgress:=[blue]False[/blue]

And it seems to be working like a charm. The lines I commented were those related to showing the MsgBox - the variables and all, I don't need them, b/c they were only used for the MsgBox.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top