Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the outlook folders other than default ones (like Inbox, Sent) using python win32com?

This is how I am able to access the inbox:

   outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
   inbox = outlook.GetDefaultFolder("6")

When I tried to access the user created folders in Outlook using the below code:

   outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
   Folder = outlook.Folders[1]
   print (Folder)

I got this error:

  raise IndexError("list index out of range")

IndexError: list index out of range

Any help would be appreciated.

like image 834
soldy Avatar asked Dec 07 '22 15:12

soldy


1 Answers

Globally, you can do:

from win32com.client import Dispatch
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
root_folder = outlook.Folders.Item(1)

Then you can check the name of this folder by

print (root_folder.Name)

And to know the names of the subfolders you have:

for folder in root_folder.Folders:
    print (folder.Name)

Finally, let's say you want to access a subfolder named folder_of_soldy in your root_folder, you do:

soldy_folder = root_folder.Folders['folder_of_soldy']

And so on if you have other subfolders in folder_of_soldy.

Hope you find what you need

like image 70
Ben.T Avatar answered Jan 17 '23 08:01

Ben.T