Platinum Partner

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.
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}