# Win32Ole, MAPI, and Outlook

**URL:** <https://www.ruby-forum.com/t/win32ole-mapi-and-outlook/132497>\
**Category:** Ruby\
**Created:** [March 20, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/win32ole-mapi-and-outlook/132497 "2008-03-20T19:22:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [March 20, 2008, 7:22pm UTC](https://www.ruby-forum.com/t/win32ole-mapi-and-outlook/132497/1 "2008-03-20T19:22:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [March 20, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/win32ole-mapi-and-outlook/132497/2 "2008-03-20T21:55:29Z")

</div>

On Thu, Mar 20, 2008 at 12:21 PM, Jeff M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## follows: end
> 
> Posted via [http://www.ruby-forum.com/](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)

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [March 20, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/win32ole-mapi-and-outlook/132497/3 "2008-03-20T22:16:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [March 25, 2008, 6:42am UTC](https://www.ruby-forum.com/t/win32ole-mapi-and-outlook/132497/4 "2008-03-25T06:42:55Z")

</div>

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
