DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Automating Outlook With Ruby: Inbox & Messages
From the <a href="http://rubyonwindows.blogspot.com">Ruby on Windows</a> blog.
require 'win32ole'
outlook = WIN32OLE.new('Outlook.Application')
mapi = outlook.GetNameSpace('MAPI')
# Get a reference to the Inbox or other folder:
inbox = mapi.GetDefaultFolder(6)
personal_folders = mapi.Folders.Item('Personal Folders')
baseball_folder = personal_folders.Folders.Item('Baseball')
# Get a count of a folder's unread items:
puts "#{inbox.UnreadItemCount} unread messages"
# Iterate over messages in a folder:
inbox.Items.each do |message|
# Your code here...
end
# Retrieve a single message:
first_message = inbox.Items(1)
# Delete a message:
message.Delete
# Move a message to another folder:
baseball_folder = personal_folders.Folders.Item('Baseball')
message.Move(baseball_folder)
inbox.Items.Count.downto(1) do |i|
message = inbox.Items(i)
if message.Subject =~ /cardinals/i
message.Move(baseball_folder)
end
end
Further details and discussion can be found <a href="http://rubyonwindows.blogspot.com/2007/08/automating-outlook-with-ruby-inbox.html">here</a>.





