Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Automating Outlook with Ruby: Inbox & Messages

From the Ruby on Windows 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 here.

Automating Outlook with Ruby: Address Books

From the Ruby on Windows 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 here.

Automating Outlook with Ruby: Contacts

From the Ruby on Windows blog...
require 'win32ole'

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

#   Create a new Contact
contact = outlook.CreateItem(2)
    contact.FullName = 'Stan Musial'
    contact.BusinessTelephoneNumber = '(314)555-1234'
    contact.Email1Address = 'stan_the_man@stlcardinals.com'
contact.Save

#   Iterate over Contact Items and extract data
contacts = mapi.GetDefaultFolder(10).Items
contacts.each do |contact|
    puts contact.FullName
    puts contact.Email1Address
    puts contact.BusinessTelephoneNumber
end

Further details can be found here.

Automating Outlook with Ruby: Tasks

From the Ruby on Windows blog...
require 'win32ole'

outlook = WIN32OLE.new('Outlook.Application')

#   creating a new task
task = outlook.CreateItem(3)
task.Subject = 'Order Yankees Playoff Tickets'
task.Body = 'Order first-round playoff tickets...'
task.ReminderSet = true
task.ReminderTime = '07/19/2007 9:00 AM'
task.DueDate = '07/20/2007'
task.ReminderPlaySound = true
task.ReminderSoundFile = 'C:\Windows\Media\Ding.wav'
task.Save

#   getting tasks data
mapi = outlook.GetNameSpace('MAPI')
tasks = mapi.GetDefaultFolder(13)
for task in tasks.Items.Restrict("[Status] = 'Waiting on someone else'")
    puts task.Subject
    puts task.DueDate
    puts task.PercentComplete
    puts task.Status
    puts task.Importance
    puts task.LastModificationTime
end

#   search for and delete tasks
for task in tasks.Items.Restrict("[Subject] = 'Order Yankees Playoff Tickets'")
    task.Delete
end

Further details can be found here.

Automating Outlook with Ruby: Calendar Appointments

From the Ruby on Windows blog.

Use Ruby to extract appointments data from the Microsoft Outlook Calendar, delete an appointment, and create a new appointment.
require 'win32ole'

outlook = WIN32OLE.new('Outlook.Application')
mapi = outlook.GetNameSpace('MAPI')
calendar = mapi.GetDefaultFolder(9)

#   reading calendar data
data = []
calendar.Items.each do |appointment|
    rec = []
    rec << appointment.Subject
    rec << appointment.Location
    rec << appointment.Start
    rec << appointment.Duration
    rec << appointment.End
    rec << appointment.Body
    data << rec
end

#   deleting an appointment
calendar.Items.each do |appointment|
    if appointment.Subject == 'Punch Bud Selig in the Nose'
        appointment.Delete
    end
end

#   creating a new appointment
appointment = outlook.CreateItem(1)
appointment.Start = '7/29/2007 11:00 AM'
appointment.Duration = 300
appointment.Subject = 'Baseball Hall of Fame Induction'
appointment.Body = 'Tony Gwynn and Cal Ripken Jr.'
appointment.Location = 'Cooperstown, NY'
appointment.ReminderMinutesBeforeStart = 15
appointment.ReminderSet = true
appointment.Save

Further details and discussion can be found here.

Automating Outlook with Ruby: Sending Email

From the Ruby on Windows blog.

Here's an example of how to use Win32 OLE/COM automation to create and send an email message with Microsoft Outlook.
require 'win32ole'
outlook = CreateObject('Outlook.Application')
message = outlook.CreateItem(0)
message.Subject = 'Subject line here'
message.Body = 'This is the body of your message.'
message.To = 'ted.williams@redsox.com'
message.Attachments.Add('c:\my_folder\my_file.txt', 1)
message.Send

Further details can be found here.

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS