【发布时间】:2018-12-01 01:59:45
【问题描述】:
在使用 win32com 遍历 Outlook 中的特定文件夹时,我试图将遇到的任何附件的文件名作为字符串返回。当附件不是“Outlook 项目”(https://i.imgur.com/UauktNV.png)时,将文件名作为字符串返回时我没有任何问题——不幸的是,这是我实际上必须处理的唯一类型的附件。我面临的错误如下......
File "C:/Users/bob/PycharmProjects/program/program.py", line 76, in handle_email
attachment = attachments.Item(1)
File "<COMObject <unknown>>", line 2, in Item
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Array index out of bounds.', None, 0, -2147352567), None)
我的代码如下
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
root_folder = outlook.Folders.Item(1)
test = root_folder.Folders['test']
messages = test.items
attachments = message.Attachments
attachment = attachments.Item(1)
name = attachment.FileName
如果我打印“附件”,我也会得到 <COMObject <unknown>>。这让我相信我可能没有从属性对象层次结构中接近对象,正如这里引用的 https://docs.microsoft.com/en-us/office/vba/api/outlook.attachment 虽然 AFAIK 它似乎是正确的,但可能是为什么当我尝试注释掉 attachment 和 then 打印 name 我收到错误 AttributeError: <unknown>.FileName。
注意:我的问题和这个类似 --> Filename is returned as null when an outlook item (.msg file) is added as attachment to an outlook email sent
但是,我什至无法打印空值,而且我尝试读取的附件有文件名。
谢谢。
【问题讨论】: