Create an Outlook rule via script

Hi experts.

I saw vbs code for creating an outlook 2010 rule. http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_28345013.html#a39806489 for example.

I need code for a rule that will move mails with a subject line starting with ***SPAM*** to the folder SPAM.

What what the code be, I couldn't figure it out myself.
LVL 61
McKnifeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ltlbearand3Commented:
I have made some modifications to BlueDevilFan's Script.  Give this a try:

'On the next line, edit the name of the rule as desired
Const RULE_NAME = "Move SPAM to SPAM Folder"
Const olRuleReceive = 0
Const olFolderInbox = 6
Dim olkApp, olkSes, olkCol, olkRul, olkCD1, olkCD2, olkMRA
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNamespace("MAPI")
olkSes.Logon olkApp.DefaultProfileName
Set olkCol = olkSes.DefaultStore.GetRules()
Set olkRul = olkCol.Create(RULE_NAME, olRuleReceive)
Set olkCD1 = olkRul.Conditions.Subject
With olkCD1
    .Enabled = True
    .Text = Array("***SPAM***")
End With
Set olkMRA = olkRul.Actions.MoveToFolder
With olkMRA
    set .Folder = olkSes.GetDefaultFolder(olFolderInbox).Parent.Folders("SPAM")
    .Enabled = True
End With
olkCol.Save False
olkSes.Logoff	
olkApp.Quit
Set olkMRA = Nothing
Set olkCD2 = Nothing
Set olkCD1 = Nothing
Set olkRul = Nothing
Set olkCol = Nothing
Set olkSes = Nothing
Set olkApp = Nothing
WScript.Quit

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
McKnifeAuthor Commented:
Hi ltlbearand3.

I created the folder SPAM as subfolder of the inbox. But the script errors back "Object not found" Line: 18, Char: 5
So it seems, the folder is not addressed correctly, but I don't see it. Does it work for you?
0
ltlbearand3Commented:
This script assumes SPAM is a folder on the same level as the inbox.  Since it is a subfolder of the inbox, try this instead for line 18:

    set .Folder = olkSes.GetDefaultFolder(olFolderInbox).Folders("SPAM")

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

McKnifeAuthor Commented:
Great. Of course that was the error - solved.
May I ask you for a little favor? This script part of a little deployment, the Spam folder gets created and the rule. To make the script perfect, I would like to suppress the error message that we see if the SPAM folder already exists. How would I need to modify the lines that I have:
Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

Set objNewFolder = objFolder.Folders.Add("SPAM")

Open in new window

0
McKnifeAuthor Commented:
Thanks!
About the "favor" I asked for: found it myself, add
on error resume next

Open in new window

before line 7
0
ltlbearand3Commented:
McKnife,

Glad that worked and you found an answer for your other question.  Just a reminder to make sure you turn error handling back on after you try to add the folder.  For example:

On Error Resume Next
Set objNewFolder = objFolder.Folders.Add("SPAM")
On Error Goto 0

Open in new window

0
McKnifeAuthor Commented:
Oh, another small glitch: I'd like to stop the rule creation if the rule already exists. Could you give me a hint on how to do that, please?
0
ltlbearand3Commented:
That may be something that should be another post, but it is simple enough:

Add this code:
    Dim oCurrentRule As Outlook.Rule
    For Each oCurrentRule In olkCol 
        If oCurrentRule.Name = "SPAM" Then Exit Sub
    Next

Open in new window


In front of this line:
Set olkRul = olkCol.Create(RULE_NAME, olRuleReceive)

Open in new window


By using this method you should be able to remove the temporary disabling of the error handler.
0
McKnifeAuthor Commented:
Thanks, but it doesn't work but throws
Line 10, Char22
Error: expected end of statement
Code: 800A0401
Script code is
'On the next line, edit the name of the rule as desired
Const RULE_NAME = "Move SPAM to SPAM Folder"
Const olRuleReceive = 0
Const olFolderInbox = 6
Dim olkApp, olkSes, olkCol, olkRul, olkCD1, olkCD2, olkMRA
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNamespace("MAPI")
olkSes.Logon olkApp.DefaultProfileName
Set olkCol = olkSes.DefaultStore.GetRules()
    Dim oCurrentRule As Outlook.Rule
    For Each oCurrentRule In olkCol 
        If oCurrentRule.Name = "SPAM" Then Exit Sub
    Next
Set olkRul = olkCol.Create(RULE_NAME, olRuleReceive)
Set olkCD1 = olkRul.Conditions.Subject
With olkCD1
    .Enabled = True
    .Text = Array("***SPAM***")
End With
Set olkMRA = olkRul.Actions.MoveToFolder
With olkMRA
    set .Folder = olkSes.GetDefaultFolder(olFolderInbox).Folders("SPAM")
    .Enabled = True
End With
olkCol.Save False
olkSes.Logoff	
'olkApp.Quit
Set olkMRA = Nothing
Set olkCD2 = Nothing
Set olkCD1 = Nothing
Set olkRul = Nothing
Set olkCol = Nothing
Set olkSes = Nothing
Set olkApp = Nothing
WScript.Quit

Open in new window

I guess the Rule name needs to be adjusted too, but that's not the error here since I have already tried that.
0
ltlbearand3Commented:
Sorry I forgot to convert one piece.  
Instead of:
Dim oCurrentRule As Outlook.Rule

Open in new window

Try this:
    Dim oCurrentRule

Open in new window

0
McKnifeAuthor Commented:
Next error. line 12, Char 69, invalid exit statement
0
ltlbearand3Commented:
Sorry, I was testing in Outlook VBA instead of vbscript.  Since this is vbscript, it would be better to convert the code as follows (I also fixed the rule name issue that you found):

'On the next line, edit the name of the rule as desired
Const RULE_NAME = "Move SPAM to SPAM Folder"
Const olRuleReceive = 0
Const olFolderInbox = 6
Dim olkApp, olkSes, olkCol, olkRul, olkCD1, olkCD2, olkMRA, oCurrentRule, blnFound
Set olkApp = CreateObject("Outlook.Application")
Set olkSes = olkApp.GetNamespace("MAPI")
olkSes.Logon olkApp.DefaultProfileName
Set olkCol = olkSes.DefaultStore.GetRules()

' Look to see if Rule Exists
blnFound = False
For Each oCurrentRule In olkCol 
  If oCurrentRule.Name = RULE_NAME Then blnFound = True
Next

' If the Rule has not been found, add it
If not blnFound Then
	Set olkRul = olkCol.Create(RULE_NAME, olRuleReceive)
	Set olkCD1 = olkRul.Conditions.Subject
	With olkCD1
	    .Enabled = True
	    .Text = Array("***SPAM***")
	End With
	Set olkMRA = olkRul.Actions.MoveToFolder
	With olkMRA
	    set .Folder = olkSes.GetDefaultFolder(olFolderInbox).Folders("SPAM")
	    .Enabled = True
	End With
	olkCol.Save False
End If

olkSes.Logoff	
olkApp.Quit
Set olkMRA = Nothing
Set olkCD2 = Nothing
Set olkCD1 = Nothing
Set olkRul = Nothing
Set olkCol = Nothing
Set olkSes = Nothing
Set olkApp = Nothing

WScript.Quit

Open in new window

0
McKnifeAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.