McKnife
asked on
Create an Outlook rule via script
Hi experts.
I saw vbs code for creating an outlook 2010 rule. https://www.experts-exchange.com/questions/28345013/Deploy-rule-for-Outlook-2010.html?anchorAnswerId=39806489#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.
I saw vbs code for creating an outlook 2010 rule. https://www.experts-exchange.com/questions/28345013/Deploy-rule-for-Outlook-2010.html?anchorAnswerId=39806489#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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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")
ASKER
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:
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")
ASKER
Thanks!
About the "favor" I asked for: found it myself, add
About the "favor" I asked for: found it myself, add
on error resume next
before line 7
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:
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
ASKER
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?
That may be something that should be another post, but it is simple enough:
Add this code:
In front of this line:
By using this method you should be able to remove the temporary disabling of the error handler.
Add this code:
Dim oCurrentRule As Outlook.Rule
For Each oCurrentRule In olkCol
If oCurrentRule.Name = "SPAM" Then Exit Sub
Next
In front of this line:
Set olkRul = olkCol.Create(RULE_NAME, olRuleReceive)
By using this method you should be able to remove the temporary disabling of the error handler.
ASKER
Thanks, but it doesn't work but throws
Line 10, Char22
Error: expected end of statement
Code: 800A0401
Script code is
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
I guess the Rule name needs to be adjusted too, but that's not the error here since I have already tried that.
Sorry I forgot to convert one piece.
Instead of:
Instead of:
Dim oCurrentRule As Outlook.Rule
Try this: Dim oCurrentRule
ASKER
Next error. line 12, Char 69, invalid exit statement
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
ASKER
Thanks!
ASKER
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?