Learn about Windows PowerShell
Hey, Scripting Guy! My manager does not like appointment reminders; therefore every meeting request she sends is without reminders. I like reminders and I try to remember to turn them on, but sometimes I forget; that means that if I'm not on top of my schedule, I can miss a meeting. How can I write a script that will go through all my meetings and appointments and add a reminder to them?-- PS
Hey, PS. Well, today is Thursday. Or so we think; to tell you the truth, after you’ve stayed up till the wee hours of the morning several nights in a row, well, at some point the days all kind of meld together.
And yes, that includes the weekends, too. How do the Scripting Guys plan to spend their time off this coming weekend? The same way most people plan to spend their time off this coming weekend: testing hundreds and hundreds of Perl, VBScript, and Windows PowerShell scripts, scripts submitted as part of the on-going 2008 Winter Scripting Games. (The Games run until March 3rd, which means there’s sill plenty of time to have some fun and maybe win yourself a ton of great prizes.)
Note. Limit of one per person. That means one prize, not one ton.
Interesting enough, the Scripting Son refers to his father and his colleagues as “nerds.” Nerds?!? The Scripting Guys are definitely not nerds; after all, no self-respecting nerd would spend an entire weekend testing hundreds and hundreds of Perl, VBScript, and Windows PowerShell scripts.
But do you know what the best thing is about the Scripting Games? (And no, believe it or not it’s not staying up until 2:00 AM testing scripts that calculate the amount of water required to fill a swimming pool.) Instead, the best thing about the Scripting Games is the fact that the Scripting Guys get to do all the Scripting Games-related work plus our regular jobs; it’s like working two full-time jobs but only getting paid for one! Have you ever wondered how Microsoft managed to sock away so much money? Well, now you know.
Editor’s Note: Okay, we all know how overworked the Scripting Guy who writes this column is. Although, would you like to take a guess as to who had the idea for the Scripting Games in the first place? And who wanted to add even more scripting languages? And who wants to start traveling around the world during the Scripting Games next year? And who insists we need to get even more people to enter the Scripting Games? (Well, okay, that last one would be both the nerds – er, Scripting Guys.)
Speaking of our regular jobs, let’s take time out from the Scripting Games to show you a script that can set a reminder on all your Microsoft Outlook appointments and meetings:
Const olFolderCalendar = 9
Set objOutlook = CreateObject("Outlook.Application")Set objNamespace = objOutlook.GetNamespace("MAPI")Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)
Set colItems = objFolder.Items
For Each objItem in colItems objItem.ReminderSet = True objItem.ReminderMinutesBeforeStart = 15 objItem.SaveNext
As you can see, this is actually a pretty simple little script. We start out by defining a constant named olFolderCalendar and setting the value to 9; this tells the script which Outlook folder we want to work with. After defining the constant we create an instance of the Outlook.Application object, then use the following line of code to bind to the MAPI namespace:
Set objNamespace = objOutlook.GetNamespace("MAPI")
Scripting trivia time. As it turns out, the MAPI namespace is the only namespace you can bind to when using Microsoft Outlook. Nevertheless you still need to call the GetNamespace method and explicitly bind to the MAPI namespace. Leave this line of code out and your script will be doomed to fail.
That’s right: doomed.
Once we’ve made the connection to the MAPI namespace we can call the GetDefaultFolder method to bind to the Outlook calendar (we know we’ll be binding to the Calendar because we pass GetDefaultFolder the constant olFolderCalendar):
Set objFolder = objNamespace.GetDefaultFolder(olFolderCalendar)
As soon as we’re hooked up to the calendar we can retrieve a collection of all our calendar items (meetings and appointments) by using the following line of code:
Now we’re ready for action. In order to set a reminder on all our calendar items we first need to set up a For Each loop that walk us through all the items in the collection; that’s what this line of code is for:
For Each objItem in colItems
Inside that loop, the first thing we do for each appointment is set the value of the ReminderSet property to True; that’s going to attach a reminder to each of our appointments. We then set the value of the ReminderMinutesBeforeStart property to 15; in other words, we’re going to get a reminder 15 minutes before our appointment/meeting is scheduled to begin. Need more warning (or maybe less warning) than that? That’s fine; you can set ReminderMinutesBeforeStart to any value you desire. For example, this line of code causes the reminder to pop 30 minutes before start time:
objItem.ReminderMinutesBeforeStart = 30
Believe it or not, we’re almost done; all we have to do now is call the Save method and we’ll have added a reminder to the first item in our collection:
And then it’s back to the top of the loop, where we repeat the process with the next item in the collection.
This all works pretty well, but there are a couple things you should be aware of. For one, this script will set a reminder on all your appointments and meetings, even those that have already taken place. For example, if you run the script as-is be prepared for a dialog box to pop up and remind you of 76 previous engagements, engagements that are now way overdue. Do we know for sure that this will happen? Let’s put it this way: you’re listening to the voice of experience on this one.
Note. Is there a way to work around that? Sure; take a peek at thisHey, Scripting Guy! column to see how you filter appointments by start date.
Second, this script will set a reminder on all your appointments (and, of course, each reminder will be set to pop up 15 minutes before start time). It’s possible you don’t want reminders on all your appointments, or maybe you want different reminder times for different meetings. That’s beyond what we can talk about in today’s column. However, we do have an Office Space article that discusses filtering in some depth; that’s your ticket to selectively picking which appointments to add a reminder to (e.g., all those scheduled by your manager) and which ones to leave alone.
Oh, what the heck; it would be easier to just show you some sample code, wouldn’t it? The following script filters out any appointments that have a Start date earlier than today (that is, the appointments have already occurred); it also filters out any appointments that already have a reminder set. (That way you won’t override a 1-hour reminder with your 15-minute reminder.) We thought some of you might find this useful:
strFilter = "[ReminderSet] = False AND [Start] >= '2/21/2008'"Set colFilteredItems = colItems.Restrict(strFilter)
For Each objItem In colFilteredItems objItem.ReminderSet = True objItem.ReminderMinutesBeforeStart = 15 objItem.SaveNext
That should do it, PS. Right now it’s time for the Scripting Guy who writes this column to get back to scoring events for the 2008 Winter Scripting Games. Remember, there’s still plenty of time to submit scripts for Events 3 and 4; the deadline isn’t until tomorrow morning (February 22nd) at 8:00 AM Pacific Standard Time, which means there’s still plenty of time to earn a coveted Scripting Games Certificate of Excellence.
You say you have one last question? Does the Scripting Guy who writes this column rely on Outlook reminders to tell him when it’s time to get back to work? No, he doesn’t. Instead he just waits for the Scripting Editor to stand in the doorway, arms crossed, and eyes glaring. That’s usually reminder enough that it’s time to get back to work. Which, now that we mention it ….
CAn you write a script that can run as a rule that looks at each appointment as it arrives and sets the reminder to 30 minutes?
Hey Scripting Guy,
I used to be an IT guy, but now am going to Med School. The staff here set up our calendar without reminders (most likely to make life more difficult for us). Now with the help of your script I will be on time for everything. You rock!
Very useful article. This is exactly what I needed, thanks!
@VeeKay Awesome, I am glad you liked the script. Thanks for your comment.
This is sweetness. Thanks for sharing the script. Is it possible to reference a SharePoint 2007 calendar that I have linked to in Outlook rather than the default calendar? If so, how?
Or you can just go to options and set 30 mins as a default reminder
I work for a law firm, and one of the attorneys decided, spur of the moment, that he wanted all of his calendar appointments to remind him a day before. This script did just that, and made me look brilliant! Thank you, much.