Folder management - israel-dryer/Outlook-Python-Tutorial GitHub Wiki
It's important to know how to navigate folders in Outlook. This will prove very useful when you want to perform mail searches, organize your mail into different folders, or send mail that you have saved in the drafts folder.
NameSpace
In order to access folders in Outlook you'll need to use the NameSpace
object, which provides methods and properties that are designed for this. There are many other things that you can do with the Namespace
object, but I'll explore those in more detail in future tutorials.
Create the Outlook instance as before to get started; then use the GetNameSpace
method of the outlook instance to get the Namespace
object. You'll pass "MAPI" as the argument, which is the only argument that the GetNameSpace
method accepts for the Outlook instance.
import win32com.client as client
# create outlook instance
outlook = client.Dispatch('Outlook.Application')
# get the namespace object
namespace = outlook.GetNameSpace("MAPI")
Default Folders
There are two primary ways to access folders in Outlook... the first is with the GetDefaultFolder
method, and the second is with indexing.
GetDefaultFolder. This method accepts as an argument any one of a list of enumerations or codes that stand for a default folder type. For example the default folder type for drafts is 16
, the inbox is 6
, etc... This method returns a folder
object.
# get the default drafts folder
drafts = namespace.GetDefaultFolder(16)
inbox = namespace.GetDefaultFolder(6)
The Folders Collection Object
You'll notice that the Namespace
object has a Folders
property, which represents a collection of all the folders contained in the NameSpace
. Another way of accessing the drafts folder is to explicitly index it from the Folders
object. You can index in several ways... so whichever way you find more convenient or useful at the time is the one you should use.
For example, let's say that I have a folder called "python-mail" in my inbox, and it happens to be the first of many folders in my inbox. Indexing a folder from one of these methods returns a Folder
object. Here are a few ways in which I can access that folder object:
# by name
pyfolder = inbox.Folders['python-mail']
# by index starting at zero
pyfolder = inbox.Folders[0]
# by index starting at one
pyfolder = inbox.Folders.Item(1)
In addition to these methods, you can also use the Folders
object as an iterator
for folder in inbox.Folders:
print(folder.Name)
Some useful properties:
Folders.Count
: the number of sub-folders contained in the folder
Some useful methods:
Folders.Add(name, folder_type)
: adds a folder to the existing folder (folder type is optional)
The Folder Object
The Folder
object is a collection of outlook items, and has methods and properties associated with it as well.
Some useful properties:
Folder.Name
: the folder name as a string.Folder.Description
: the folder description; if available.Folder.FolderPath
: shows the folder path of the current folder.Folder.Parent
: access the parent folder object.Folder.Items
: a collection of outlook items (messages, appointments, contacts).
Some useful methods:
Folder.Delete()
: removes the folder.Folder.MoveTo(destination)
: moves the folder to a destination folder.
Here is a demonstration of adding a folder, moving it, and then deleting it.
# create a new folder and save a reference to it
new_folder = inbox.Folders.Add('new-folder')
# move the new folder into the "python-mail" folder
pyfolder = inbox.Folders['python-mail']
new_folder.MoveTo(pyfolder)
# delete the folder
new_folder.Delete()