In our last episode, we used Ruby to extract appointments data from the Outlook Calendar, delete an appointment, and create a new appointment. Today we'll look at managing Outlook Tasks with Ruby.
Getting Existing Tasks
As usual, we'll use the win32ole library to create a new instance (or connect to a currently running instance) of the Outlook application object:
require 'win32ole'
outlook = WIN32OLE.new('Outlook.Application')
Next we'll get the MAPI namespace:
mapi = outlook.GetNameSpace('MAPI')
Outlook consists of several Folder objects, including Inbox, Tasks, and Calendar. To get a folder object, call the MAPI object's GetDefaultFolder method, passing it an integer representing the folder to return. For the Tasks folder, this number is 13:
tasks = mapi.GetDefaultFolder(13)
The Tasks folder's Items method returns a collection of all tasks, which you can iterate over. The following code prints out some of the most-often used properties for each task:
for task in tasks.Items
puts task.Subject
puts task.Body
puts task.DueDate
puts task.PercentComplete
puts task.Status
puts task.Importance
puts task.LastModificationTime
end
The Status method/property returns an integer, representing one of the following values:
0 -- Not started
1 -- In progress
2 -- Complete
3 -- Waiting on someone else
4 -- Deferred
The Importance method/property returns an integer, representing one of the following values:
0 -- Low
1 -- Normal
2 -- High
Filtering Your Tasks Collection
You can filter your collection of Task items by calling the Items collection's Restrict method, passing it a filter string. The following code gets all 'Order Yankees World Series Tickets' tasks, and then deletes each one:
for task in tasks.Items.Restrict("[Subject] = 'Order Yankees World Series Tickets'")
task.Delete
end
Other possible filters include:
tasks.Items.Restrict("[Status] = 'Not started'")
tasks.Items.Restrict("[Importance] = 'High'")
As illustrated in the above example, to delete a Task, call its Delete method.
Creating New Tasks
To create a new task, call the Outlook Application object's CreateItem method, passing it the number 3, which represents a Task Item. This returns a new Task object:
task = outlook.CreateItem(3)
Then set values for various properties of the new Task object:
task.Subject = 'Send flowers to wife'
task.Body = 'Call flower shop and send roses.'
task.ReminderSet = true
task.ReminderTime = '7/19/2007 12:00 PM'
task.DueDate = '7/20/2007 12:00 PM'
task.ReminderPlaySound = true
task.ReminderSoundFile = 'C:\Windows\Media\Ding.wav'
When you've completed setting property values, call the Task object's Save method:
task.Save
Further details can be found in this Microsoft TechNet article.
As always, feel free to post a comment here or send me email with questions, comments, or suggestions.
Thanks for stopping by!
3 comments:
Hi,
How do you set a filter to find all items with a Subject starting with some phrase?
Thanks,
Vidar
How can I create a recurring task?
To create a recurring task, create the task as shown above, then get a reference to its Recurrence pattern and edit its properties. For example:
olRecursDaily = 0
olRecursWeekly = 1
olRecursMonthly = 2
olRecursYearly = 5
pattern = task.GetRecurrencePattern()
pattern.RecurrenceType = olRecursMonthly
pattern.PatternStartDate = '10/13/2017 12:00 PM'
pattern.NoEndDate = true
Post a Comment