繁体   English   中英

使用Python和Win32com从Outlook检索电子邮件时出现突然错误

[英]Sudden error when retrieving emails from Outlook using Python and win32com

编辑:解决了它!

我在下面添加了此代码段,以便跟踪主收件箱文件夹的位置

用于Outlook中的文件夹文件夹:打印(文件夹)

这突显了基础Outlook结构中的某些更改,并且Folder [0]不再有效。 我现在将调整代码以使其更健壮并动态选择文件夹

结束编辑

我写了一些代码从Outlook中提取电子邮件并保存附件。 直到几天前,它都可以完美工作。

我没有触摸代码,因此只能假定Outlook中的某些内容已更改。 我在公司环境中工作,因此可以进行软件的远程更新。

有谁知道这个错误是什么意思,为什么突然出现? 我很沮丧,因为在打ic之前代码工作得很好。 另外,还有使用Python从Outlook中检索电子邮件和附件的更好方法吗?

import win32com.client


def main():
    pass


def saveAttachments():
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")  # Opens Microsoft Outlook
    mailbox = outlook.Folders[0]  # Based off email address
    inbox = mailbox.Folders["Inbox"]
    emails = inbox.Items
    emails.Sort("[ReceivedTime]", True)

    destPath = "\\\\servername\\path\\"

    try:
        for mail in emails:
            if ("Detailed MTM," in mail.subject) and (mail.Attachments.Count > 0):
                print(mail.Sender)
                print(mail.Subject)
                print(mail.Receivedtime)
                attachments = mail.Attachments
                for file in attachments:
                    if "MTMDetailed" in str(file):
                        file.SaveAsFile(destPath + str("MTMDetailed.xls"))
                break
    except:
        file = open(destPath + "error.log", "w")
        file.write("Problem")
        file.close()

if __name__ == '__main__':
    main()
    saveAttachments()

  File "C:\Tools\Python\lib\site-packages\win32com\client\dynamic.py", line 256, in __getitem__
    return self._get_good_object_(self._oleobj_.Invoke(dispid, LCID, invkind, 1, index))
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The attempted operation failed.  An object could not be found.', None, 0, -2147221233), None)

我添加了代码以遍历Outlook.Folder无需依赖特定的硬编码位置即可找到我需要的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM