Mapi Attachment method WriteToFile throws exception ...
Peter Curran
pcurran at intraspect.com
Tue Jun 24 13:02:20 EDT 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
pcurran at intraspect.com (Peter Curran) wrote in message news:<bf49679f.0306231512.57ad4475 at posting.google.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
More information about the Python-list
mailing list