0

I could use win32client to read msg file yesterday, but all of a sudden it throws an error today while I'm running the same code against same file.

Couldn't find the reason. There is not much explanation about the error message. There was an Internet outage yesterday which had been restored later, but it doesn't seem to be a cause.

import win32com.client as win32

outlook = win32.Dispatch('Outlook.Application').GetNameSpace('MAPI')
msg = outlook.OpenSharedItem('path/file.msg')
print(msg.SenderName)

When it worked yesterday, the output was apparently a name, such as Joe Doe, but today it is an error message pasted below.

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "We can't open 'RE Your Sumo Invoice - Gas.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)
Billie_H
  • 69
  • 7
  • A new finding is that, I restart the kernel and run the script. It can read the file I mentioned (A.msg). BUT, when I read another file (B.msg), the error message comes again, after that I read the A.msg again, it gives the error then. – Billie_H Aug 23 '19 at 04:29
  • Did you every solve this? I am having a similar issue. The only difference in the error message is the end: `right-click the file folder, then click Properties.", None, 0, -2147287008), None)` – Foggy Nov 13 '20 at 22:49

2 Answers2

1

The error code (STG_E_FILENOTFOUND) and the error description mean you did not specify the full path to the MSG file, just the file name ('RE Your Sumo Invoice - Gas.msg').

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • The msg file is placed in the same folder with the script, so I didn't include full path. The same error message is thrown when I put full path. ```python com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "We can't open 'C://Users/sliu/Desktop/Script/Email%20scrapper/Your%20Sumo%20Invoice%20-%20Elect...'. 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, -2147287037), None) ``` – Billie_H Aug 23 '19 at 04:26
  • This is a different error - STG_E_PATHNOTFOUND, which means the folder name is wrong. Indeed, you are url-encoding it. Get rid of %20 and replace them with spaces. – Dmitry Streblechenko Aug 23 '19 at 05:54
  • The string I parser in is actually 'C:/Users/sliu/Desktop/Script/Email scrapper/'. A very weird thing is that the test file I used sometimes work and sometimes doesn't. It seems not like a path error. – Billie_H Aug 23 '19 at 07:21
  • Well, the error is very unambiguous, at least when it comes to the %20 parts; they can only come from the string you pass it. Do check what you pass to OpenSharedItem. – Dmitry Streblechenko Aug 23 '19 at 16:15
  • hi, does anyone has a solution to this? I noticed when I read another msg from another folder and then re-read the failing file again, it will work. Somehow it seems outlook.OpenSharedItem() is being used not closed after the last session. Please help. – user3782604 Mar 01 '21 at 06:49
  • Please post a new question. – Dmitry Streblechenko Mar 01 '21 at 21:17
0

Firstly, you have to:

  • Rename the files so that they don't contain any special characters (white spaces, brackets, ...)
  • Specify full path to the files

There is also the issue of the file staying open once you accessed it. I haven't yet figured out how to close it from Python, but what works is closing Outlook first (including the background process) before you try to access the file again.

  • Thanks for posting! I have definitely opened up emails with special characters like spaces, hyphens, and # signs. Here is an example of how to close outlook: [https://stackoverflow.com/questions/54424638/how-to-quit-outlook-on-win32com](https://stackoverflow.com/questions/54424638/how-to-quit-outlook-on-win32com) – Foggy Jan 28 '22 at 18:41