'Loop on outlook folders and save all emails into local drive

I now know I can specify emails from which Outlook folders to save to my local drive using the following code:

inbox = outlook.GetDefaultFolder(6).Folder("Inbox").Folder("folder name").Folder("sub-folder name")

Additional query: Is it possible to specify to save emails from ALL Outlook folder to my local drive?

from win32com.client import Dispatch
import os
import re
os.chdir("c:/Users/username/Desktop/Emails")

def save_emails():
    for message in inbox.Items:
        if message.Class == 43:
            name = str(message.Subject)
            # to eliminate any special characters in the name
            name = re.sub('[^A-Za-z0-9]+', '', name) + '.msg'
            # to save in the current working directory
            message.SaveAs(os.getcwd() + '//' + name)


if __name__ == '__main__':
    outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
    inbox = outlook.GetDefaultFolder(6)
    save_emails()


Solution 1:[1]

Yes, it is possible to save all items from all folders. You can iterate over all folder recursively in the code and repeat the same actions with each folder. Here is C# example how you could do that (the Outlook object model is common for all kind of programming languages):

private void EnumerateFoldersInDefaultStore()
{
    Outlook.Folder root =
        Application.Session.
        DefaultStore.GetRootFolder() as Outlook.Folder;
    EnumerateFolders(root);
}

// Uses recursion to enumerate Outlook subfolders.
private void EnumerateFolders(Outlook.Folder folder)
{
    Outlook.Folders childFolders =
        folder.Folders;
    if (childFolders.Count > 0)
    {
        foreach (Outlook.Folder childFolder in childFolders)
        {
            // Write the folder path.
            Debug.WriteLine(childFolder.FolderPath);
            // Call EnumerateFolders using childFolder.
            EnumerateFolders(childFolder);
        }
    }
}              

See Enumerate folders for more information.

Solution 2:[2]

Try to loop on all folders and subfolders

Example

from win32com.client import Dispatch
import os
import re


def outlook_folder(parent_folder):
    print(parent_folder.Name)

    if parent_folder.Items.Count > 0:
        items = parent_folder.Items
        print(items.Count)

        for i in reversed(range(items.Count)):
            if items[i].Class == 43:
                print(items[i])
                save_name = re.sub('[^A-Za-z0-9]+', " ", items[i].Subject) + ".msg"
                items[i].SaveAs(os.getcwd() + '//' + save_name)

    if parent_folder.Folders.Count > 0:
        for folder in parent_folder.Folders:
            outlook_folder(folder)


if __name__ == '__main__':
    outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
    inbox = outlook.GetDefaultFolder(6)
    outlook_folder(inbox)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eugene Astafiev
Solution 2