【发布时间】:2021-12-07 12:30:38
【问题描述】:
我有一个脚本可以移动/保存/分类共享邮件中的电子邮件。当有时找不到邮件文件夹时,我会遇到错误。我正在使用以下函数来检索邮件文件夹。有时它有效,有时则无效。一旦抛出错误,我通常必须重新启动 Outlook 才能使其工作。它有时无法在Inbox 中找到子文件夹,并返回它是空的。
是否有办法确保始终找到子文件夹?
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))
我在另一篇我认为与我的问题有关的帖子中发现了这个,但我真的不明白它在说什么 stackoverflow.com/a/40853098/10833061
【问题讨论】:
-
我们能看到您调用
get_folder的代码吗,例如outlook_instance设置为什么?
标签: python-3.x outlook pywin32 win32com