Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Mapi Attachment method WriteToFile throws exception ...

Reply
Thread Tools

Re: Mapi Attachment method WriteToFile throws exception ...

 
 
Peter Curran
Guest
Posts: n/a
 
      06-24-2003
Answering my own thread here in case someone else is interested. For
some reason CDO doesn't support what I was trying to do through MAPI,
or there's a bug or something, so I ended up using the Outlook object
model instead of the MAPI one.

To find the documentation of the object model, search for vbaol*.chm
on your disk - it'll probably show up under the office directory. In
case your file name is different, you might just search your office
dir for *.chm. The object model (javadoc-esque) makes this a million
times easier. The python code is just slightly different - here's the
(working) result.

"""
file - getvwstofile.py
opens a folder in an exchange inbox, iterates over its contents, saves
attachments on messages to disk
"""
from win32com.client.dynamic import Dispatch

s = Dispatch("Outlook.Application")
space = s.GetNameSpace("MAPI")
inbox = space.GetDefaultFolder(6)
vw = inbox.Folders[9] #open the folder i want

#now iterate over the messages
for x in range(1, len(vw.Items) + 1):
sub = vw.Items[x].Subject
sub = sub.encode('ascii', 'strict')
bContinue = 0
z = sub.find("Alert, ")
#if the subject the string i want in the subj, continue
if z != -1:
item = vw.Items[x]
attaches = item.Attachments
if attaches:
for i in range(1, len(attaches) + 1):
atitem = attaches[i]
if(atitem):
atitem.SaveAsFile("c:\\vwmsgs\\%d.html" % (x))
s = None






(Peter Curran) wrote in message news:<. com>...
> Hello -
>
> I'm getting an exception when I try to save an email attachment
> gathered from an Exchange server using Mapi and python com utils.
>
> The COM exception I'm getting back in indecipherable (to me) and I'm
> hoping that someone in this group can help me dereference the error so
> that I can move on. My code is included below - it's ugly but it
> works well other than where I try to save the attachment - maybe
> someone can use it.
>
> Is there a place where I can look up CDO exceptions? Has anyone else
> resolved this problem?
>
> The exception:
>
> File "<COMObject <unknown>>", line 2, in WriteToFile
> File "C:\Python22\Lib\site-packages\win32com\client\dynamic.py",
> line 237, in _ApplyTypes_
> result = apply(self._oleobj_.InvokeTypes, (dispid, LCID, wFlags,
> retType, argTypes) + args)
> com_error: (-2147352567, 'Exception occurred.', (17389, 'Collaboration
> Data Objects', ' [Collaboration Data Objects - [E_FAIL(80004005)]]',
> None, 0, -2147467259), None)
>
>
> The code:
>
> """
> file - getvwstofile.py
> opens a folder in an exchange inbox, iterates over its contents, saves
> attachments on messages to disk
> """
> import sys
>
> s = Dispatch("Mapi.Session")
> s.Logon()
> vw = s.Inbox.Folders[9] #open the folder i want
>
> #now iterate over the messages
> for x in range(1, len(vw.Messages) + 1):
> sub = vw.Messages[x].Subject
>
> bContinue = 0
> z = sub.find("Alert, ")
> #i only want messages with particular subjects
> if z != -1:
> item = vw.Messages[x]
> attaches = item.Attachments
> if attaches:
> for i in range(1, len(attaches) + 1):
> atitem = attaches[i]
>
> #here's the weirdness - got the attachment, but can't
> save it!
> #i've also tried atitem.WriteToFile("c:\vsmsgs\%s" %
> (atitem.Name))
> if(atitem):
> atitem.WriteToFile("c:\\vsmsgs\\%s" %
> (atitem.Name))
> s.Logoff()
> s = None

 
Reply With Quote
 
 
 
 
Rudy Schockaert
Guest
Posts: n/a
 
      06-24-2003
Peter Curran wrote:
> Answering my own thread here in case someone else is interested. For
> some reason CDO doesn't support what I was trying to do through MAPI,
> or there's a bug or something, so I ended up using the Outlook object
> model instead of the MAPI one.
>


Searching the MSDN database ( http://msdn.microsoft.com ) learns the
following:

The 80004005 error message indicates that your data cannot be accessed.
This error can be paraphrased as "I could not access your data for some
reason."

This could mean a lot of things (corrupt data in your mailbox for
example). What I suggest you to do is print out the subject and the
folder of the mail your accessing and then examine the mail with another
tool like OutlookSpy (try google) to find out what's wrong with it.

Rudy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is an exception specification needed if the method throws and catchesits own exception? elcapitan666@gmail.com C++ 2 11-27-2007 04:01 AM
Do you need finally when your method throws Exception.. Tomas Java 17 10-05-2005 10:40 AM
MAPI extension for non-MAPI enabled email clients? hugh jass Computer Support 0 02-11-2005 12:20 PM
when to throws or catch exception in chains of method calls? Matt Java 2 07-01-2004 02:27 PM
Exception Exception is not compatible with throws clause in Runnable.run() Chris Miller Java 4 11-22-2003 03:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57