Using win32com and/or active_directory, how can I access an email folder by name?
Question: In python w/ Outlook 2007, using win32com and/or active_directory, how can I
get a reference to a sub-folder so that I may move a MailItem to this sub-
folder?

I have an inbox structure like:

    
    
        Inbox
          |
          +-- test
          |
          `-- todo
    

I can access the inbox folder like:

    
    
    import win32com.client
    import active_directory
    session = win32com.client.gencache.EnsureDispatch("MAPI.session")
    win32com.client.gencache.EnsureDispatch("Outlook.Application")
    outlook = win32com.client.Dispatch("Outlook.Application")
    mapi = outlook.GetNamespace('MAPI')
    inbox =  mapi.GetDefaultFolder(win32com.client.constants.olFolderInbox)
    print '\n'.join(dir(inbox))
    

But when I try to get subdirectory `test` per [Microsoft's
example](http://msdn.microsoft.com/en-us/library/bb175261.aspx) the `inbox`
object doesn't have the `Folders` interface or any way to get a subdirectory.

How can I get a `Folder` object which points to `test` subdir?


Answer: I realize this is an old question but I've been using the win32com package
recently and found the documentation troublesome to say the least...My hope is
that someone, someday can be saved the turmoil I experienced trying to wrap my
head around MSDN's explanation

Here's and example of a python script to traverse through Outlook folders,
accessing e-mails where I please.

_Disclaimer_ I shifted around the code and took out some sensitive info so if
you're trying to copy and paste it and have it run, good luck.

    
    
    import win32com
    import win32com.client
    import string
    import os
    # the findFolder function takes the folder you're looking for as folderName,
    # and tries to find it with the MAPIFolder object searchIn
    
    def findFolder(folderName,searchIn):
    try:
        lowerAccount = searchIn.Folders
        for x in lowerAccount:
            if x.Name == folderName:
                print 'found it %s'%x.Name
                objective = x
                return objective
        return None
    except Exception as error:
        print "Looks like we had an issue accessing the searchIn object"
        print (error)
        return None
    
    def main():
    
    outlook=win32com.client.Dispatch("Outlook.Application")
    
    ons = outlook.GetNamespace("MAPI")
    
    #this is the initial object you're accessing, IE if you want to access
    #the account the Inbox belongs too
    one = '<your account name here>@<your domain>.com'
    
    #Retrieves a MAPIFolder object for your account 
    #Object functions and properties defined by MSDN at 
    #https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mapifolder_members(v=office.14).aspx
    Folder1 = findFolder(one,ons)
    
    #Now pass you're MAPIFolder object to the same function along with the folder you're searching for
    Folder2 = findFolder('Inbox',Folder1)
    
    #Rinse and repeat until you have an object for the folder you're interested in
    Folder3 = findFolder(<your inbox subfolder>,Folder2)
    
    #This call returns a list of mailItem objects refering to all of the mailitems(messages) in the specified MAPIFolder
    messages = Folder3.Items
    
    #Iterate through the messages contained within our subfolder
    for xx in messages:
        try:
            #Treat xx as a singular object, you can print the body, sender, cc's and pretty much every aspect of an e-mail
           #In my case I was writing the body to .txt files to parse...
            print xx.Subject,xx.Sender,xx.Body
           #Using move you can move e-mails around programatically, make sure to pass it a 
            #MAPIFolder object as the destination, use findFolder() to get the object
            xx.Move(Folder3)
    
    
        except Exception as err:
            print "Error accessing mailItem"
            print err       
    
    
    if __name__ == "__main__":
    main()
    

PS Hope this doesn't do more harm than good.

