【问题标题】:win32com Outlook subfolder not always foundwin32com Outlook 子文件夹并不总是找到
【发布时间】: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


【解决方案1】:

执行代码时 Outlook 是否始终在系统上运行?

否则,您需要创建一个新的 Outlook Application 实例并登录到配置文件才能访问文件夹。我不是 python 开发人员,所以这里有一个 C# 示例代码,它为您说明了所需的工作流程:

 Outlook.Application GetApplicationObject()
        {

            Outlook.Application application = null;

            // Check whether there is an Outlook process running.
            if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
            {

                // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
            }
            else
            {

                // If not, create a new instance of Outlook and sign in to the default profile.
                application = new Outlook.Application();
                Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
                nameSpace.Logon("", "", Missing.Value, Missing.Value);
                nameSpace = null;
            }

            // Return the Outlook Application object.
            return application;
        }

Outlook 对象模型适用于所有编程语言,因此我希望您在理解所需方法时不会遇到任何困难。在 MSDN 中的 Get and sign in to an instance of Outlook 文章中了解更多信息。

最后,不清楚来自哪里 - 我建议澄清发布的示例代码中的所有内容,以便我们提出其他建议。

【讨论】:

  • Outlook 一直在运行,我的调度功能就像您提供的示例代码一样,但由于我一直在运行 Outlook,所以我总是调度一个活动实例。我总能找到顶级文件夹,例如我自己的邮件和共享邮件,以及它的收件箱。但是当我尝试更深入时,有时会找不到那些子文件夹。
  • 我在另一篇我认为与我的问题有关的帖子中发现了这个,但我真的不明白它在说什么stackoverflow.com/a/40853098/10833061
猜你喜欢
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 1970-01-01
  • 2015-11-22
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多