首页 > 解决方案 > 使用 Python 访问 Outlook 文件夹时无法收到第一条消息

问题描述

我使用以下内容获取子文件夹的第一条消息

import win32com.client
import os
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in 
this case the inbox. You can change that number to reference
messages = inbox.Folders.Item("MyFolder")
message = messages.GetFirst()

虽然它适用于收件箱,但当我尝试获取子文件夹的第一条消息时

我收到一个属性错误:Item.GetFirst raise AttributeError("%s%s" % (self.username , attr))

该文件夹已经有消息

有任何想法吗?

标签: pythonvbaoutlookpywin32

解决方案


我将子文件夹部分更改为

messages = inbox.Folders[3].Items
message = messages.GetLast()

我测试了括号中的数字,发现在我的情况下 [3] 是理想的子文件夹


推荐阅读