View Single Post
  #33  
Old March 30th 10, 02:17 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Create a macro in Outlook to run a rule

OK, so at least some VBA code can run.

Let's try adding additional testing and error handling to see more of what's
going on here. I'm still mystified as to what's happening. You are running
in Outlook 2007, correct?

Sub RunRuleDeleteSpam2()
Dim oNS As Outlook.NameSpace
Dim oStore As Outlook.Store
Dim colRules As Outlook.Rules
Dim oRule As Outlook.Rule

Set oNS = Application.GetNamespace("MAPI")
If Not (oNs Is Nothing) Then
Set oStore = oNS.DefaultStore
If Not (oStore Is Nothing) Then
Set colRules = oStore.Rules()
If Not (colRules Is Nothing) Then
Set oRule = colRules.Item("Delete Spam")
If Not (oRule Is Nothing) Then
oRule.Execute
Else
MsgBox "Rule Delete Spam is nothing"
End If
Else
MsgBox "Rules collection is nothing"
End If
Else
MsgBox "Default Store is nothing"
End If
Else
MsgBox "NameSpace is nothing"
End If
End Sub

Try this new code that provides a little more information. I don't think it
will work or help though, I think something very odd is going on that might
not be possible to figure out via newsgroup posts.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"DLGolfs" wrote in message
...
Here is what I have in the VBA:
Sub RunRuleDeleteSpam()
Dim oNS As Outlook.NameSpace
Dim oStore As Outlook.Store
Dim colRules As Outlook.Rules
Dim oRule As Outlook.Rule

Set oNS = Application.GetNamespace("MAPI")
Set oStore = oNS.DefaultStore
Set colRules = oStore.Rules()
Set oRule = colRules.Item("Delete Spam")
oRule.Execute
End Sub

Sub Test()
MsgBox "This is a test"
End Sub

When I put the cursor at the new sub Test() it runs and a dialog box opens
that says "this is a test"
When I put the cursor at the beginning of subrunrule, then it gives me a
438
error, I hit debug and highlights the setcolrules line in the second
section.

Hope that helps you


Ads