Win32Ole, MAPI, and Outlook

Hello Everybody,
I’m trying to make a Ruby Script that adds contacts to Microsoft
outlook via Ruby’s Win32OLE and MAPI. So it works for the most part, but
I’m having trouble trying to handle exceptions… The big problem right
now is that the contacts need to go into a subfolder. However, if the
current mailbox doesn’t have the specified subfolder, it kicks an
exception. Thus, I added a line of code to add that folder. However, if
the folder doesn’t exist, it kicks an error, and if it tries to create
it when it does exist, it kicks an error. I’ve tried IF statements and
UNLESS statements, but I’m not getting anywhere… My code is as
follows:

outlook = WIN32OLE.new(‘Outlook.Application’)
mapi = outlook.GetNameSpace(‘MAPI’)
contacts_folder = mapi.GetDefaultFolder(10)

if contacts_folder.Folders(“Company Contacts”)
co_contacts = mapi.GetDefaultFolder(10).Folders(“Company Contacts”)
else
co_contacts = mapi.GetDefaultFolder(10).Folders.Add(“Company
Contacts”)
end

when contacts_folder.Folder(“Company Contacts”) does not exist, it
doesn’t do the ELSE portion of the IF statement. It kicks me the error
“The operation failed. An object could not be found”. Does anyone know
how to handle this?

Any help is appreciated!!

Thanks,

  • Jeff M.

On Thu, Mar 20, 2008 at 12:21 PM, Jeff M. [email protected]
wrote:

follows:
end

Posted via http://www.ruby-forum.com/.

Off the top of my head I would guess it is returning something other
than
nil or false which is why you aren’t hitting your else, what it would be
returning I have no idea.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

Aha!

I got it to work by using some simple exception handling. Since I hadn’t
used it before (I’m somewhat new to scripting) I didn’t really think of
it before. Anyway, in conclusion, I used this:

begin
co_contacts = mapi.GetDefaultFolder(10).Folders(“Company Contacts”)
rescue Exception
co_contacts = mapi.GetDefaultFolder(10).Folders.Add(“Company
Contacts”)
end

Thanks!

  • Jeff

The code below processes mail from the inbox downloads the attchments
then moves the mail to a subfolder.

require ‘win32ole’
require ‘yaml’

#Read Configuration
def _readconfig
config = YAML.load_file("./config.yaml")
@folder = config[“config”] [“foldername”]
@path = config[“config”] [“pathname”]
end

#Setting up outlook application instance
def _outapp
outlook = WIN32OLE.new(‘Outlook.Application’)
mapi = outlook.GetNameSpace(‘MAPI’)
@inbox = mapi.GetDefaultFolder(6)
end

#Process each email and save attachment to path configured
def _processor
proc = @inbox.Folders.Item(’#{@folder}’)
proc.Items.each do |message|
message.Attachments.each do |attachment|
filename = “#{@path}\#{attachment.FileName}”
attachment.SaveAsFile(filename)
end
end

end

_readconfig
_outapp
_processor

Jeff M. wrote:

Aha!

I got it to work by using some simple exception handling. Since I hadn’t
used it before (I’m somewhat new to scripting) I didn’t really think of
it before. Anyway, in conclusion, I used this:

begin
co_contacts = mapi.GetDefaultFolder(10).Folders(“Company Contacts”)
rescue Exception
co_contacts = mapi.GetDefaultFolder(10).Folders.Add(“Company
Contacts”)
end

Thanks!

  • Jeff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs