Win32Ole, MAPI, and Outlook

J

Jeff Miller

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 Miller
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

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 Miller
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.
 
J

Jeff Miller

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
 
B

Brian Scott

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top