I put this post together for a Twitter user going by the name DerroldJess (DJ for short). DJ tweeted me asking if I could create a script that would check a mailbox every 15 minutes for a message with a specific subject, and display an alert if it did not find one. You can view the entire conversation here. The good news is that this is possible, although not from within Outlook. In order to make a check every 15 minutes, the solution needs a timer to trigger it. The only timer-like capability in Outlook is a reminder on a task or appointment, and for this that’s impractical. If the script only needed to run once a day, then I’d have gone that route and written this as an Outlook macro. Instead, I opted to write this using VBScript. VBScript runs outside of Outlook, so DJ can use Windows’ built-in task scheduler to run the task every 15 minutes.
The script itself is really very simple. It starts by creating an instance of Outlook, then searches the Inbox folder for messages with the subject DJ is looking for. If it doesn’t find any, then it displays a dialog-box alerting DJ to that fact. If instead it finds one or more of the messages, then it checks each one of them to see if it arrived within the last 15 minutes. If one of them is less than 15 minutes old, then the script falls through and does nothing. Otherwise, it displays a dialog-box alerting DJ to the fact that no message was received in the last 15 minutes.
As always, you should test the code thoroughly to make sure it does what you want and expect before putting it into production. While I tested the code before posting it, my environment may be different than yours and I may not have completely understood the goal.
This solution requires Outlook 2007 or later. The computer must be on and Windows running for the script to work.
Code and Instructions.
- Open Notepad.
- Copy the script and paste it into Notepad.
- Edit the code as desired. I included a comment line immediately before where you can make a change.
- Save the file with a .vbs extension. You can name it anything you want.
- Close Notepad.
- Create a task using Windows Task Scheduler. Set the task to run this script.
'Create some constants 'On the next line change the name of the script as desired Const SCRIPT_NAME = "Check for Processor Report" 'On the next line edit the subject of the message the script is to look for Const SUBJECT_TO_FIND = "59145822 ACE eSurvey_15minFeed_new - Processor Report" 'On the next line change the message the script displays if it does not find a report Const DISPLAY_MSG = "I could not find a Processor Report message that arrived within the last 15 minutes." Const olFolderInbox = 6 'Create some variables Dim olkApp, olkSes, olkFld, olkCol, olkMsg, bolFound 'Connect to Outlook Set olkApp = CreateObject("Outlook.Application") Set olkSes = olkApp.GetNamespace("MAPI") olkSes.Logon olkApp.DefaultProfileName 'Connect to the inbox and check for the subject messages Set olkFld = olkSes.GetDefaultFolder(olFolderInbox) Set olkCol = olkFld.Items.Restrict("[Subject]= '" & SUBJECT_TO_FIND & "'") If olkCol.Count = 0 Then bolFound = False Else For Each olkMsg In olkCol If DateDiff("n", olkMsg.ReceivedTime, Now) <= 15 Then bolFound = True Exit For End If Next End If If Not bolFound Then MsgBox DISPLAY_MSG, vbCritical + vbOKOnly, SCRIPT_NAME End If 'Clean-up olkSes.Logoff Set olkMsg = Nothing Set olkCol = Nothing Set olkFld = Nothing Set olkSes = Nothing Set olkApp = Nothing