I got the following code:
import win32com.client
import os
directory = "C:/Users/mypath/"
for filename in os.listdir(directory):
_, file_extension = os.path.splitext(filename)
if file_extension == ".msg":
print(filename)
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
msg = outlook.OpenSharedItem(directory + filename)
att=msg.Attachments
for i in att:
i.SaveAsFile(os.path.join(directory, i.FileName))
del outlook, msg
This code extracts the files attached to a .msg and when I run it, I got the following output:
ATP-3770289.msg ATP-5126209.msg ATP-5126317.msg ATT -1937501.msg com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "We can't open 'C://Users/omar.lopez.rubio/Desktop/admisiones/ATT%20-1937501.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)
Which is caused obviously because
ATT -1937501.msg
has a space. I'm running this on Spyder in windows. Any clues how to solve this? Thanks.