views:

332

answers:

2

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 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?

+1  A: 

This works for me to move a mail item into a "test" subdirectory (simplified by getting rid of gencache stuff):

import win32com.client

olFolderInbox = 6
olMailItem = 0

outlook = win32com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace('MAPI')
inbox =  mapi.GetDefaultFolder(olFolderInbox)

item = outlook.CreateItem(olMailItem)
item.Subject = "test"

test_folder = inbox.Folders("test")
item.Move(test_folder)
Ryan Ginstrom
This call doesn't work for me `inbox.Folders('test')`. However I did find that I could iterate over `inbox.Folders` and test against `a_folder.Name`
Ross Rogers
Also, you can find all those outlook constants in the win32com -- `win32com.client.constants.olFolderInbox`
Ross Rogers
A: 

Something that did work for me was iterating over the folder names. ( When I posted this question, I couldn't figure out the folder names ).

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)

fldr_iterator = inbox.Folders   
desired_folder = None
while 1:
    f = fldr_iterator.GetNext()
    if not f: break
    if f.Name == 'test':
        print 'found "test" dir'
        desired_folder = f
        break

print desired_folder.Name
Ross Rogers