1
votes

My code worked fine with a test msg file, let's say it is file_1.

But it didn't work with the target msg I actually intend to run on, let's say it is file_2.

The even stranger thing is that after it threw a com_error, it failed to run on file_1.

Please see details in the snippet pasted below.

I've changed it full path, doesn't work. I've closed my Outlook client, doesn't work. I've tried to use other package, couldn't install msg_extract, with an error message about another package named OLE something. Found a package called outlook_msg, which could parse my msg file in, but there is not many functions, and I could not even retrieve email receiver, so I should probably stick with win32com.

from win32com import client as win32

outlook = win32.Dispatch('Outlook.Application').GetNamespace('MAPI')
msg = outlook.OpenSharedItem('file_1.msg')
print(msg.SenderEmailAddress)

[email protected]

outlook = win32.Dispatch('Outlook.Application').GetNamespace('MAPI')
msg = outlook.OpenSharedItem('file_2.msg')
print(msg.SenderEmailAddress)

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "We can't open 'file_2.msg'. It's possible the file is already open, or you don't have permission to open it.\n\nTo check your permissions, right-click the file folder, then click Properties.", None, 0, -2147287038), None)

outlook = win32.Dispatch('Outlook.Application').GetNamespace('MAPI')
msg = outlook.OpenSharedItem('file_1.msg')
print(msg.SenderEmailAddress)

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "We can't open 'file_1.msg'. It's possible the file is already open, or you don't have permission to open it.\n\nTo check your permissions, right-click the file folder, then click Properties.", None, 0, -2147287008), None)

I expect file_2 could output a result like file_1 did in my first attempt, but it showed an error, which has been pasted above.

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "file_2.msg'. It's possible the file is already open, or you don't have permission to open it.\n\nTo check your permissions, right-click the file folder, then click Properties.", None, 0, -2147287008), None)
2

2 Answers

2
votes

It seems the .msg file is already open. Try pasting the code before your script so that if any .msg file is open it would kill it first.

Script:

import os

try:
    os.system('taskkill /f /im OUTLOOK.exe')
except:
    pass

... snippet ... your script goes here.
0
votes

The error is STG_E_FILENOTFOUND, which makes sense - you are only specifying the filename without a path. You must provide a fully qualified file name.