【问题标题】:(4096, 'Microsoft Outlook', 'The attempted operation failed. An object could not be found.', None, 0, -2147221233), None) - error in outlook(4096, 'Microsoft Outlook', '尝试的操作失败。找不到对象。', None, 0, -2147221233), None) - Outlook 中的错误
【发布时间】:2021-05-31 07:19:55
【问题描述】:

我目前正在使用 pywin32.com python 库将邮件移动到 Outlook 中的特定文件夹。 我必须使文件夹路径可配置,因此我创建了一个 python 文件,我将文件夹路径保存为列表。 FoldersConfig.py 文件内容为:

#相对于收件箱文件夹的文件夹名称

mailBox = "mailbox-name"
nagiosDestinationFolder = ["Resolved_Clients","Internal","Nagios alerts"] #all these folders are been created in outlook with exact names.

然后有另一个文件可以将邮件移动到目标文件夹。就我而言,Nagios alerts 是我的目标文件夹。 MoveNagiosAlerts.py 文件的代码 sn-p 为:

outlook = client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    stores = outlook.Stores
    for store in stores:
        mailBox = FoldersConfig.mailBox
        if(mailBox in store.DisplayName):
            rootFolder = store.GetDefaultFolder(6) #rootFolder is the Inbox folder
            break 
    
    def moveWarnings():
        print("Moving warning mails...")
        warnCount = 0
        scriptingDictionary = {}
        nagiosDestFolder = rootFolder
        for i in FoldersConfig.nagiosDestinationFolder:
            nagiosDestFolder = nagiosDestFolder.Folders(i)
        print(nagiosDestFolder)
        for i in range(rootFolder.Items.Count-1,0,-1):
            msg = rootFolder.Items[i]
            if(msg.Body.find("State: WARNING") != -1):
               msg.Move(nagiosDestFolder)
               warnCount+=1
        return warnCount

我运行 MoveNagiosAlerts.py 文件时弹出的错误是:

Root folder(Inbox):  Inbox
Moving warning mails..
Traceback (most recent call last):
  File "C:\Users\Documents\MoveNagiosAlerts.py", line 193, in <module>
    main()
  File "C:\Users\Documents\MoveNagiosAlerts.py", line 172, in main
    warningsCount = moveWarnings()
  File "C:\Users\Documents\MoveNagiosAlerts.py", line 56, in moveWarnings
    nagiosDestFolder = nagiosDestFolder.Folders(i)
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\win32com\client\dynamic.py", line 181, in __call__
     return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.defaultDispatchName,None)
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 structure for your refrence

我的问题是为什么会抛出这个错误?它有时可以正常工作,但大多数时候会抛出此错误。知道我的代码到底有什么问题吗?还是前景的问题?

据我了解,我认为 pywin 客户端无法获取列表中的文件夹。但不确定是不是这个原因。

【问题讨论】:

    标签: python outlook


    【解决方案1】:

    我注意到代码中有以下循环:

    for i in range(rootFolder.Items.Count-1,0,-1):
                msg = rootFolder.Items[i]
    

    当项目被移动时,集合中的项目数量会减少。所以,你需要为此做好准备。

    此外,如果您只想移动符合条件的项目,您需要使用Find/FindNextRestrict 方法来获取此类项目,并且它们会在不检查任何其他属性的情况下移动所有这些项目。在以下文章中详细了解这些方法:

    【讨论】:

    • 感谢您抽出宝贵时间回答我的问题@Eugene Astafiev。如果我没记错的话,“rootFolder.Items.Count”会采用收件箱中的当前项目数。这两个链接非常有帮助。感谢您的宝贵时间。
    猜你喜欢
    • 2018-02-06
    • 2018-03-10
    • 1970-01-01
    • 2017-01-10
    • 2018-10-19
    • 2021-10-27
    • 1970-01-01
    • 2019-07-31
    • 2017-10-01
    相关资源
    最近更新 更多