win32com: Accessing MAPIOBJECT

Joel usenet at prettyhipprogramming.com
Wed Mar 12 17:47:13 EST 2003


I've been tinkering with win32com in the hopes of being able to convert 
some of the mail in some of my Outlook folders into a mbox for use with 
SpamBayes. I've figured out how to access them messages, but they seem to 
only have the interfaces that Outlook itself needs: Body, Subject, etc, and 
not the full headers for example.  

I assume that to get to the complete data of the message, I need to somehow 
access the MAPIOBJECT. Each message does have an attribute called 
MAPIOBJECT with a method called QueryInterface().  My question is that now 
I'm not sure what I'm supposed to do.  QueryInterface calls for a CLSID but  
which one?

Below is the code I've got so far. As it stands now, it walks through all 
of the folders in Outlook printing out the subject of each message.

########################################

from win32com.client import Dispatch
import pythoncom

def ol_walk(Folders, depth = 0):

    for fnum in range(Folders.Count):
        msgs = Folders.Item(fnum+1).Items

        spacer = " " * (5 * depth)
        dash = "-" * depth

        print "%sFolder: %s Number of Messages: %d" % \
            (dash, Folders.Item(fnum+1).Name, msgs.Count)
        
        if msgs.Count:
            #msgs[n] is also valid instead of msgs.Item(n)            

            for mnum in range(msgs.Count):            
            #for mnum in range(1):
                try: print "%s Subject: %s" % \
                        (spacer, msgs.Item(mnum+1).Subject.encode())
                except UnicodeError, msg:
                    print "%s Unicode Error: %s" % (spacer, msg)        
            
        ol_walk(Folders.Item(fnum+1).Folders, depth + 1)
    
ol = Dispatch("Outlook.Application")
ns = ol.GetNamespace("MAPI")
ol_walk(ns.Folders)

######################################

Thanks
Joel




More information about the Python-list mailing list