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

David has posted 23 posts at DZone. View Full User Profile

Automating Outlook With Ruby: Address Books

08.28.2007
| 4137 views |
  • submit to reddit
        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 list of available Address Lists
mapi.Session.AddressLists.each do |list|
    puts list.Name
end

#   Access an Address List:
address_list = mapi.Session.AddressLists('Contacts')
address_list = mapi.Session.AddressLists('Personal Address Book')
address_list = mapi.Session.AddressLists('Global Address List')

#   Outlook security dialog will prompt to allow access to AddressEntries:
address_entries = address_list.AddressEntries

#   Iterate over the AddressEntries collection:
address_entries.each do |address_entry|
    if address_entry.Name =~ /Sinatra/
        puts address_entry.Name, address_entry.Address
    end
end

#   Search for an Address:
address_entry = address_entries.Item("sinatra, frank")
puts address_entry.Name
puts address_entry.Address
Further details can be found <a href="http://rubyonwindows.blogspot.com/2007/08/automating-outlook-with-ruby-address.html">here</a>.