Re: Mapi Attachment method WriteToFile throws exception ...

Discussion in 'Python' started by Peter Curran, Jun 24, 2003.

  1. Peter Curran

    Peter Curran Guest

    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
    if(atitem):
    atitem.SaveAsFile("c:\\vwmsgs\\%d.html" % (x))
    s = None






    (Peter Curran) wrote in message news:<>...
    > 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
    >
    > #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
    Peter Curran, Jun 24, 2003
    #1
    1. Advertising

  2. 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
    Rudy Schockaert, Jun 24, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Miller
    Replies:
    4
    Views:
    13,347
    Chris Smith
    Nov 22, 2003
  2. Matt
    Replies:
    2
    Views:
    470
    Chris Smith
    Jul 1, 2004
  3. Tomas
    Replies:
    17
    Views:
    2,867
    steve
    Oct 5, 2005
  4. Replies:
    2
    Views:
    334
  5. justineee
    Replies:
    0
    Views:
    333
    justineee
    Oct 29, 2008
Loading...

Share This Page