0

I tried to use this script I found here. What I want to do is given either some subject line or email I will download and save the attachment.

This is the code I used:

import datetime
import os
import win32com.client


path = os.path.expanduser("//cottonwood//users///MyDocuments//Projects//outlook crawler")

today = datetime.date.today()

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) 
messages = inbox.Items

def saveattachemnts(subject):
    for message in messages:
        if message.Subject == subject and message.Unread or message.Senton.date() == today:
            # body_content = message.body
            attachments = message.Attachments
            attachment = attachments.Item(1)
            for attachment in message.Attachments:
                attachment.SaveAsFile(os.path.join(path, str(attachment)))
                if message.Subject == subject and message.Unread:
                    message.Unread = False
                break

saveattachemnts("Snarf")

I am getting this error:

  File "<COMObject <unknown>>", line 2, in Item
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Array index out of bounds.', None, 0, -2147352567), None)

The Outlook email is a work email and it is Microsoft Outlook 2010.

My question is how do I download and save attachments from Microsoft Outlook.

0m3r
  • 12,286
  • 15
  • 35
  • 71
justanewb
  • 133
  • 4
  • 15
  • Please clarify what exactly your question is. Have you done any debugging? – AMC Apr 07 '20 at 18:47
  • 1
    @AMC I did try debugging it but I am not sure what is wrong. I edited my post to include a question. – justanewb Apr 07 '20 at 20:03
  • _My question is how do I download and save attachments from Microsoft outlook._ Take a look at this: https://stackoverflow.com/questions/39656433/how-to-download-outlook-attachment-from-python-script – AMC Apr 07 '20 at 20:16
  • Does this answer your question? [How to download outlook attachment from Python Script?](https://stackoverflow.com/questions/39656433/how-to-download-outlook-attachment-from-python-script) – AMC Apr 07 '20 at 20:16
  • 1
    @AMC no, it doesn't address the error I am getting. – justanewb Apr 07 '20 at 20:58

1 Answers1

2

Work with Items.Restrict Method (Outlook) to filter by subject line and attachment. see Filtering Items

import win32com.client

Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)

Filter = ("@SQL=" + chr(34) + "urn:schemas:httpmail:subject" +
                    chr(34) + " Like 'Snarf' AND " +
                    chr(34) + "urn:schemas:httpmail:hasattachment" +
                    chr(34) + "=1")

Items = Inbox.Items.Restrict(Filter)
for Item in Items:
    for attachment in Item.Attachments:
        print(attachment.FileName)
        attachment.SaveAsFile(r"C:\\subfolder\\" + attachment.FileName")

Filtering Items Using a String Comparison that DASL filters support includes equivalence, prefix, phrase, and substring matching. Note that when you filter on the Subject property, prefixes such as "RE: " and "FW: " are ignored.

0m3r
  • 12,286
  • 15
  • 35
  • 71
  • That seems to work although, I get this error: (4096, 'Microsoft Outlook', "Cannot save the attachment. You don't have appropriate permission to perform this operation.", None, 0, -2147024891), None). – justanewb Apr 08 '20 at 18:43
  • I am guessing I need to get the permission or something? – justanewb Apr 08 '20 at 18:44
  • Make sure you have path with file name, c:\filename.pdf or what ever and I think maybe try using different folder not just c:\ drive- Example c:\folder\filename.pdf @justanewb – 0m3r Apr 08 '20 at 18:59
  • I changed your code to attachment.SaveAsFile(r"C:\subfolder\filename.pdf" + attachment.FileName) and it worked but it does not save it to the specific file that I give it. – justanewb Apr 08 '20 at 20:37
  • 1
    I get this: ^ SyntaxError: EOL while scanning string literal – justanewb Apr 08 '20 at 20:44
  • 1
    Okay try this `attachment.SaveAsFile("C:\\subfolder\\" + attachment.FileName) ` @justanewb – 0m3r Apr 08 '20 at 20:50