I'm trying to set up a script that creates a mail rule, but I'm having some issues with it. Here's the skinny:
I work at a university. We've got a set up now where all students receive "Student Announcements" emails, which are basically 30-50 spam shit mails a day that clutter everyone's mailbox. Students always complain about it, and most have no idea how to create mail rules or anything to stop them from hitting their inbox.
So, what our main network guy has come up with is a script that will create a subfolder in everyone's inbox called "Student Announcements", and a mail rule that takes any of those said Student Announcement emails and puts them in that folder.
The rule itself shouldn't be hard to come up with at all. I can easily create it via Outlook's built in Rules thingie. 2 problems seem to come into play when trying to script it, though:
1) The mails don't come from an email address. I'm not big on how it works as I'm basically just Help Desk, but my understanding is that the mails actually come from some Exchange folder with the display name "Student Out". If they came from an email address, I'd have had this done awhile ago.
2) I'm pretty much brand new to vb scripting. As such, I've got the rule broken into 2 parts--a .vbs file that creates the folders, and a .bas that has to be added to Outlook that allows you to run a macro that creates the rule. I'd love to figure out how to get this all into one .vbs so we can run it easily across the network, but I'm having a ton of trouble doing this.
---
With that out of the way, I've got a working version of the rule that I created for myself. Outlook describes the rule as:
With Student Out in the Sender's Address
Move to the Student Announcements folderThe script I've got can create a rule, but the rule reads as follows in Outlook after it's been created:
From Student Out
Move to the Student Announcements folderThat slight difference causes the rule not to work. I've only been doing this VB scripting stuff for a little while, and I've been trying unsuccessfully to get it working correctly. Here's the meat and potatoes of my code:
spoiler (click to show/hide)
Dim colRules As Outlook.Rules
Dim oRule As Outlook.Rule
Dim testRule As Outlook.Rule
Dim colRuleActions As Outlook.RuleActions
Dim oMoveRuleAction As Outlook.MoveOrCopyRuleAction
Dim oFromCondition As Outlook.ToOrFromRuleCondition
Dim oInbox As Outlook.Folder
Dim oMoveTarget As Outlook.Folder
'Specify target folder for rule move action
Set oInbox = Application.Session.GetDefaultFolder(olFolderInbox)
'Assume that target folder already exists.
'This can be changed to any existing subfolder
'of the inbox
Set oMoveTarget = oInbox.Folders("Student Announcements")
'Get Rules from Session.DefaultStore object
Set colRules = Application.Session.DefaultStore.GetRules()
'Create the rule by adding a Receive Rule to Rules collection
Set oRule = colRules.Create("Student Annoncements Rule", olRuleReceive)
'Specify the condition in a ToOrFromRuleCondition
'Condition is if the mail is from 'Student Out'
'send the mail to the Student Out Test folder
Set oFromCondition = oRule.Conditions.From
With oFromCondition
.Enabled = True
.Recipients.Add ("Student Out")
.Recipients.ResolveAll
End With
'Specify the action in a MoveOrCopyRuleAction object
'Action is to move the message to the target folder (Student Announcements)
Set oMoveRuleAction = oRule.Actions.MoveToFolder
With oMoveRuleAction
.Enabled = True
.Folder = oMoveTarget
End With
'Update the server and display progress dialog
colRules.Save
I don't know if perhaps my rule condition shouldn't be a ToOrFromRuleCondition or if there's something I can do so that it works looks for "Student Out" in the sender's address.
Any help would be awesome Drinky.