OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

(4096, 'Microsoft Outlook', 'The operation failed.', None, 0, -2147287037) error when saving an email to my local machine

  • Thread starter Thread starter Prakhar Rathi
  • Start date Start date
P

Prakhar Rathi

Guest
I am trying to download some emails from my outlook folder to my local machine as .msg files using the following code.

Code:
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}")

I get a com_error on the same. The stack trace is below:

Code:
---------------------------------------------------------------------------
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)

I have looked at a few potential ways to do this but I am not able to solve for it. Any solutions would be appreciated.
<p>I am trying to download some emails from my outlook folder to my local machine as <code>.msg</code> files using the following code.</p>
<pre class="lang-py prettyprint-override"><code>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}")
</code></pre>
<p>I get a <code>com_error</code> on the same. The stack trace is below:</p>
<pre><code>---------------------------------------------------------------------------
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)
</code></pre>
<p>I have looked at a few <a href="https://stackoverflow.com/questions...ttempted-operation-failed-an-object-could-not">potential ways</a> to do this but I am not able to solve for it. Any solutions would be appreciated.</p>
 

Latest posts

Top