Outlook 2013 VBA to run a rule

Not open for further replies.

Gary Brown

Outlook version
Outlook 2013 32 bit
Email Account
I have a macro that worked on my old PC but doesn't work on my new PC.
Its an Outlook 2013 32bit VBA module that runs an excising rule in the current folder.
The only difference is my new PC is 64bit both running Windows 7 ultimate.
For clarity my old PC was 32bit.
When initiated I get:
Run-time error '-2147221233 (80040 10f)':
When I run debug I get:
The code gets stuck on line: Next (marked below in red)

The code is as follows:

Sub RunAllInboxRules()
Dim st As Outlook.Store
Dim myRules As Outlook.Rules
Dim rl As Outlook.Rule
Dim runrule As String
Dim rulename As String
rulename = "Spam"
Set st = Application.Session.DefaultStore
Set myRules = st.GetRules
Set cf = Application.ActiveExplorer.CurrentFolder
For Each rl In myRules
If rl.RuleType = olRuleReceive Then
If rl.Name = rulename Then
rl.Execute ShowProgress:=True, Folder:=cf
runrule = rl.Name
End If
End If
ruleList = "Rule was executed correctly:" & vbCrLf & runrule
MsgBox ruleList, vbInformation, "Macro: Spam_Rule_Finished"
Set rl = Nothing
Set st = Nothing
Set myRules = Nothing
End Sub

Any advise and I would be extremely grateful...

Gary (Manchester UK)
It works here. Does the rule exist with the name using proper case? (It doesn't error for me if the rules doesn't exist, so that may not be a factor.) Erroring on Next usually means the problem is with the For but I can't repro it.

does the error message include any other text?
Hi Diane,

Thank you so much for taking the time...
Yes the rule named Spam exists.
The only other message I get is in 'Microsoft Visual Basic':
Run-time error '92':
For loop not initialized
Then I hit debug and it highlights the line/word Next
How annoying these needle's in hey stacks are...

proof attached ;)
in chronicle order 1, 2, 3 & 4...


  • 2.PNG
    54.3 KB · Views: 597
  • 1.PNG
    15.4 KB · Views: 628
  • 3.PNG
    39 KB · Views: 624
  • 4.PNG
    47.8 KB · Views: 596
The error 'For loop not initialized.' Means you jumped into the middle of a For...Next loop. It doesn't look like that is happening though. Second cause would be an object in the loop isn't available - which points to a problem with the call to a folder - that looks good. Maybe the problem is with one of the rules it runs?
Two things I see, 1) the variable cf is not declared, 2) you can leave the loop after the "spam" rule was run.

I don't think the first issue has anything to do with the error, however, it's a basic cause for any unexpected errors. In order to avoid that write
Option explicit
to the very first line of the module. You can have the IDE do that automatically for new modules by checking Tools/Options -> Declaration for variables necessary (or similar).

Maybe the second issue helps avoiding the error. As you want to run only one rule, use a loop like this:
for each ...
  if rule.name="spam" then
     exit for
Hi Michael,

Again firstly thanks for your time... but as I'm only guessing at just what you mean I really haven't a clue where to copy and paste as what I've tried/guessed just gives even more errors.
All I want is for the script to run one of my existing rules in the current folder...

ps. The rule runs perfectly when manually ran in Rules and Alerts/Run Rules Now...
As to the for each loop, all you need to apply to your existing code is the Exit For command.
I put the Exit For command after the Next and it finished without errors but it didn't run my rule.
That makes no sense. Putting it after the Next you should get a compiler error.
your correct, sorry I put it before the Next ie
Exit For
As you'll of deduced I really haven't any idea of the logic... I do apologise. As said it then executes with no errors and displays my finishing message but doesn't actually run my 'Spam' rule...
If you want to write code, you need to think about its logic. The meaning of 'If... Then...' is pretty simple.

If you look at my basic sample, you'll see that it exits the loop only if the If statement is met. If you'd put the Exit For outside any If statement, then it'd leave the loop after the first cycle without any condition.
nothing wrong with the VBA script it was an error in the rule it was attempting to run...
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
A Outlook 2013 - VBA - Cahnging background colour Outlook VBA and Custom Forms 4
J Outlook 2013 crashes saving VBA & clicking tools | digital signature Outlook VBA and Custom Forms 1
H Problems With Outlook 2013 VBA To Send and Print an email Outlook VBA and Custom Forms 1
R Outlook 2013 VBA I want to put the entire message to clipboard Using Outlook 5
J VBA code can't be completely executed in outlook 2013 Using Outlook 0
K Outlook 2013 - Use VBA to change Due date of Tasks Using Outlook 3
P now on office 365 but getting error messages about missing Outlook 2013 cache folders Using Outlook 2
P Outlook 2013 search no longer works Using Outlook 2
K Closing external IMAP email... Outlook 2013 Using Outlook 0
P Outlook 2013 Calendar Archiving Using Outlook 0
T Compuserve, Yahoo, Oath2 and Outlook 2013 Using Outlook 4
Paula S. Outlook 2013 won't recognize new password Using Outlook 4
B Outlook 2013 erratically deleting original file that is attached Using Outlook 0
X Using Outlook 2013 and Outlook 365 Using Outlook 1
I Error saving screenshots in a custom form in outlook 2016, outlook 365 - ok in outlook 2013, outlook 2010 Outlook VBA and Custom Forms 5
J Outlook 2013 Change color of text in data fields of contacts in Outlook 2013? Using Outlook 10
Commodore Outlook (2013) issues with icloud.com mail Using Outlook 0
D iCloud Add-in not working in Outlook 2013 and Outlook 2016 After Windows Upgrade & iCloud Upgrade Using Outlook 2
P Outlook 2013 "Item could not be moved - still an issue for Outlook 2013 Using Outlook 0
M Creating an RSS Feed **FROM** Outlook 2013 Calendar. Using Outlook 5
D Outlook 2013 Yahoo IMAP Sync problems Using Outlook 1
iwshim outlook 2013 - I cannot see the "Manage Add-ins" Using Outlook 2
B Outlook 2013/Exchange 2013 - Conf Rooms not fully booking "resolved conflict" meetings Using Outlook 3
G Can't create Folder Groups in Outlook 2013 Using Outlook 0
A Run time error 424. object required in outlook 2013 Outlook VBA and Custom Forms 10
Rupert Dragwater Several questions regarding Outlook 2013 Using Outlook 9
P Outlook 2013 opens with FILE tab selected. Using Outlook 3
I How to display sender's name instead of email address in outlook 2013 message Using Outlook 5
Z Outlook 2013 Gmail IMPA emails not showing dates only showing times Using Outlook 1
B Forward every other email in Outlook 2013 Outlook VBA and Custom Forms 2
P Synching Outlook 2013 Notes with iPhone 8 Using Outlook 1
N Outlook 2013 Folder View Using Outlook 8
M Outlook 2013 fails to start -- missing WindowsCodecs.dll error Using Outlook 3
David Michaels Contact card shows blank Outlook 2013 Outlook VBA and Custom Forms 0
J Calendar data can be viewed on Outlook.com, but no longer syncs to Outlook 2013 Using Outlook.com accounts in Outlook 3
C Attached files on Outlook 2010 Sp1 & Exc 2013 are missing Exchange Server Administration 1
R Folders shifting around in Outlook 2013 and 2016 Using Outlook 3
P Sync Desktop Outlook 2013 with Outlook.com? Using Outlook.com accounts in Outlook 5
J Outlook 2013 when clicking To in a new email show Company names Using Outlook 0
J Outlook - 2013 - Error msg when copying folders from Online Archives to another user's mailbox Using Outlook 0
D Outlook 2013 changing iCloud reminder time? Using Outlook 0
J Object Variable or With Block Not Set Error in Outlook 2016 and not Outlook 2013 Outlook VBA and Custom Forms 3
P Outlook 2013 puts replies in Outbox Using Outlook 1
P Contacts gone missing in Outlook 2013 POP Using Outlook 4
M Outlook 2013 Using Outlook 1
P MS OUTLOOK 2013 - Adding Sender on the CC line Using Outlook 5
P MS OFFICE 2013 - Outlook locking up when using the sent on behalf of feature in VB Using Outlook 3
I Outlook 2010, 2013 will not open .msg or .eml files Using Outlook.com accounts in Outlook 1
Jeff Davis Help fix my Outlook 2013? Using Outlook 28

Similar threads