I have a script that moves/saves/categorizes emails in a shared mail. And I'm encountering errors when sometimes mail folders are not found. I'm using below function to retrieve mail folders. And sometimes it works and othertimes it doesn't. I usually have to restart outlook to make it work once the error has been thrown. It sometimes fails to find the subfolders within the Inbox
and it returns that it is empty.
Is there anyway to ensure that the subfolders are always found?
OUTLOOK = win32com.client.GetActiveObject('Outlook.Application') #This is how I dispatch outlook
Mapi - folders
['x', 'y', 'z',]
x - folders
['PersonMetadata', 'Yammer Root', 'Tasks', 'Sent Items', 'Outbox', 'Notes', 'Junk Email', 'Journal', 'Inbox', 'Files', 'ExternalContacts', 'Drafts', 'Deleted Items', 'Conversation History', 'Conversation Action Settings', 'Contacts', 'Calendar', 'Archive']
Inbox - folders
['a', 'b', 'c'] #THIS IS SOMETIMES EMPTY
FOLDERS = {"example": ["x", "Inbox", "a"]}
def get_folder(outlook_instance, *folders):
target_folder = outlook_instance
try:
for folder in folders:
print(folder_path)
print([f.Name for f in target_folder.Folders])
target_folder = target_folder.Folders[str(folder)]
except Exception:
raise ValueError("".join(f".Folders[{folder}]" for folder in folders))
return target_folder
def get_outlook_folder(folder, outlook):
mapi = outlook.GetNamespace("MAPI")
return get_folder(mapi, *FOLDERS.get(folder))
I found this on an other post that I think relates to my issue, but I don't really understand what it is saying stackoverflow.com/a/40853098/10833061