(4096, 'Microsoft Outlook', '操作失败。', None, 0, -2147287037) 将电子邮件保存到本地计算机时出现错误

问题描述 投票:0回答:1

我正在尝试使用以下代码将一些电子邮件作为

.msg
文件从我的 Outlook 文件夹下载到我的本地计算机。

messages = source_folder.Items
for message in messages:
    # Estimate the size of the email
    size = message.Size
    if size < 5 * 1024 * 1024:  # 5MB
        # Save the email as a .msg file
        subject = message.Subject.replace(":", "").replace("\\", "").replace("/", "").replace("*", "").replace("?", "").replace("\"", "").replace("<", "").replace(">", "").replace("|", "")
        received_time = message.ReceivedTime.strftime("%Y%m%d%H%M%S")
        file_name = f"{subject}.msg"
        file_path = os.path.join(email_dir, file_name)
        time.sleep(10)
        message.SaveAs(file_path)  # 3 corresponds to the .msg format
print(f"Emails have been saved to {email_dir}")

我也得到了

com_error
。堆栈跟踪如下:

---------------------------------------------------------------------------
com_error                                 Traceback (most recent call last)
Cell In[17], line 12
     10     file_path = os.path.join(email_dir, file_name)
     11     time.sleep(10)
---> 12     message.SaveAs(file_path)  # 3 corresponds to the .msg format
     13 if i == 0: 
     14     break

File <COMObject <unknown>>:2, in SaveAs(self, Path, Type)

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The operation failed.', None, 0, -2147287037), None)

我已经研究了一些潜在的方法来做到这一点,但我无法解决它。任何解决方案将不胜感激。

python-3.x automation outlook
1个回答
0
投票

-2147287037
STG_E_PATHNOTFOUND
,这很可能意味着文件夹 (
email_dir
) 不存在。

© www.soinside.com 2019 - 2024. All rights reserved.