运行时错误'91通常是由于未正确引用对象而导致的。在保存电子邮件附件时,可能会发生这种错误。以下是一些可能的解决方法:
MailItem.Attachments
属性来访问附件对象。attachments = mail.Attachments
Attachment.FileName
属性来获取附件的文件名。for attachment in attachments:
file_name = attachment.FileName
# 确保文件名有效
save_path = "C:\\Attachments\\"
# 确保保存路径存在并具有正确的权限
# 删除非法字符
file_name = file_name.replace("/", "").replace(":", "")
以上是一些常见的解决方法,可以帮助您解决保存电子邮件附件时出现运行时错误'91的问题。请根据您的具体情况适当调整代码示例。