Objective of the program:
I have a python script that searches my Outlook
inbox for an email with a specific subject and if found will save the attachment on the email to a specific path.
The script executes as expected when the path chosen to save the file is on my C: drive, however I would like to save the file on a shared O: drive and this results in an error when saving the attachment.
Error I am getting:
However, I get the below error
"Traceback (most recent call last): File "", line 19, in File ">", line 2, in SaveAsFile pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Cannot save the attachment. Path does not exist. Verify the path is correct.', None, 0, -2147024893), None)"
Any ideas what is causing this error?
Given the script executes as expected for the C: drive it doesn't seem like the path is invalid for the O: drive.
My research effort:
Below is the code:
import win32com.client
import os
import datetime
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
now = datetime.datetime.now().strftime("%Y %m %d")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
file_path_that_works = "C:/Users/kennedj/Desktop/projects"
file_path_that_does_not_work = "O:/projects"
for message in messages:
if message.Subject == 'Specific Subject':
attachments = message.Attachments
for attachment in attachments:
new_file_name = 'excel attachment {}.xls'.format(now)
attachment.SaveAsFile(os.path.join(file_path_that_does_not_work , new_file_name))
break
message.Delete()