1

I am trying to use Python 3.6 to copy/sync sub-folders (folders below the default folders) from Outlook 2010 to a directory outside Outlook. I know how to do this with the default folders (Inbox, Drafts, Sent Items, etc...) as the default folders can be accessed via methods detailed in the answer from Bobby for this question. For example, I have written code that copies email from my inbox to an arbitrary directory:

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items

for message in messages:
    try:
        path='C:\\test1\\'+message.Subject.replace('/','-').replace(':','-')+'.msg'
        message.SaveAs(path)
    except:
        print('Error with '+message.Subject)

del outlook, message

However, I need help accessing lower directories. Below is a screen-cap of what I'm talking about.

Outlook Directories

In this image, the red arrow points to the default Inbox which the code I posted can copy emails from. The blue arrow in the image points to a directory I would like to iterate over.

In the question I linked before, Ozzius says this is possible and provides sample code though I could not get it to work for me.

Can anyone tell me how or, preferably, point me to documentation and examples about how to iterate through these sub directories? My end goal is to automate copying emails in these folders to shared drives so others working on these projects can access them.

This is my first question on Stack Overflow so please let me know if you need any more information or if this was asked in the wrong place.

1 Answers1

0

You can iterate through the subfolders of a given folder as follows.

folders = root_folder.Folders
    for folder in folders:
        print('{0} is under {1}',format(folder.Name, root_folder.Name))

You could recursively chain such a call to go through all of your sub-folders. To save an email as a .msg file, you can use the following code

mail_item.SaveAs(email_local_copy_path)
Josep
  • 563
  • 1
  • 5
  • 12