Lim Min
Lim Min

Reputation: 51

python win32com save archived outlook email

This is the code I run

import win32com.client import re

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

for account in outlook.Folders:
    if (account.Name == "[email protected]"):
        my_account = account
        break

for f in my_account.Folders:
    if f.Name == "Inbox":
        read_folder = f
        break

all_emails = read_folder.Items
email = all_emails[0]
email.SaveAs("C:/tmp/email.msg")

The last line throws an error if the message has this icon I assume this icon means it has been archived? This is the error I get:

Traceback (most recent call last):
  File "C:\anaconda3\envs\test\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-26-bd0a4750f7e3>", line 1, in <module>
    email.SaveAs("C:/tmp/email2.msg")
  File "<COMObject <unknown>>", line 3, in SaveAs
pywintypes.com_error: (-2147467260, 'Operation aborted', None, None)

Upvotes: 1

Views: 2051

Answers (2)

Andrew
Andrew

Reputation: 3

Sometimes the operation aborted messages comes from not having outlook open when you run your script. Try placing the following code before you create your outlook object.

import subprocess
subprocess.Popen(['C:\Path\To\OUTLOOK.EXE'])

Upvotes: 0

Luis Mercado
Luis Mercado

Reputation: 1

mail.SaveAs(Path=r'C:\Users\Admin\Documents\Python\Example\msg_name.msg')

Upvotes: 0

Related Questions