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

  • submit to reddit
        From the <a href="">Ruby on Windows</a> blog...
require 'win32ole'

outlook ='Outlook.Application')
mapi = outlook.GetNameSpace('MAPI')

#   Get list of available Address Lists
mapi.Session.AddressLists.each do |list|
    puts list.Name

#   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

#   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="">here</a>.