Home > Net >  Unable to save email attachment from outlook to local drive
Unable to save email attachment from outlook to local drive

Time:12-01

i have written the below code to save email attachment from outlook with specific subject but its throwing an error.

Below is the code :


import win32com.client
import os
outlook = win32com.client.Dispatch('outlook.application').GetNamespace("MAPI")
inbox = outlook.Folders('xyz.com').Folders("Inbox")
messages = inbox.Items
for msg in messages:
    if 'Subject' in msg.Subject:
       print(msg.Subject)
       if not os.path.exists('C:\xyz\reports'):
    for atch in msg.Attachments:
       atch.SaveAsFile(os.getcwd()   'C:\xyz\reports'   atch.Filename)

Error msg **: (-2147352567, 'Exception occured.',(4096, 'Microsoft Outlook', 'Cannot save the attachment. File name or directory name is not valid.' None, 0, -2147024773), None)

CodePudding user response:

Change the line

atch.SaveAsFile(os.getcwd()   'H:\Atul\Save'   atch.Filename)

to

atch.SaveAsFile('H:\Atul\Save\'   atch.Filename)
  • Related