0

I would like to download specific attachments that are coming from my Outlook inbox. I would like to filter these emails based on the subject and the date that its being sent on.

This is what I am doing:

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

 Filter = "[Subject] = 'Important Report' And [SenderEmailAddress] = 'data@cool.com' And [SentOn] 
 > '9/26/2020 08:00 AM'"


 Items = Inbox.Items.Restrict(Filter)
 Item = Items.GetFirst()

 for attachment in Item.Attachments:
 print(attachment.FileName)
 attachment.SaveAsFile(r"C:\Users\lynnette\Desktop\file.xls")

Currently, it is being filtered using [SentOn], however, I am using a specific date. I would like to filter based on the current date. I have researched Items.Restrict and could not find a Current date filter.

Any help is appreciated

L42
  • 19,427
  • 11
  • 44
  • 68
Lynn
  • 4,292
  • 5
  • 21
  • 44
  • 2
    First, you need to use a different variable name for `Filter`, it is a reserved `VBA` keyword. Btw, I added `VBA` tag cause I think this is a `VBA` code. This [answer](https://stackoverflow.com/a/64100560/2685412) already gave you the hint, you just have to combine it with your exiting subject filter which I showed in my post. – L42 Sep 29 '20 at 02:20
  • Ok I will research this and complete this. Thanks – Lynn Sep 29 '20 at 03:02
  • @L42, could you please link your post, or tell me what title to search for it? Thanks – Lynn Sep 29 '20 at 07:30
  • 1
    Here's the [link](https://stackoverflow.com/a/48318476/2685412) – L42 Oct 09 '20 at 09:42

1 Answers1

1

I believe you want to do one of this three:

Today:

Items.Restrict("@SQL=%today(\"urn:schemas:httpmail:datereceived\")%")

From the exact date:

Items.Restrict("@SQL=(\"urn:schemas:httpmail:datereceived\" >= '9/26/2020 00:00')")

Between dates:

Items.Restrict("@SQL=(\"urn:schemas:httpmail:datereceived\" >= '9/26/2020 00:00' AND \"urn:schemas:httpmail:datereceived\" <= '9/27/2020 23:59')

Microsoft documentation

UPDATE 1:

In your code you could simply use the restrict second time.

Items = Inbox.Items.Restrict(Filter)
Items = Items.Restrict("@SQL=%today(\"urn:schemas:httpmail:datereceived\")%")
Item = Items.GetFirst()
hiichaki
  • 834
  • 9
  • 19
  • Thank you @hiichaki, I am wondering how would I incorporate this in my above code for the filter? Would I need to create a new variable? Or could I place it within the ‘Items’ variable along w my current filter? – Lynn Sep 28 '20 at 15:51
  • Thank you, so it is ok to have the Items variable hold two different items? I need to research VBA as I am still learning this. Thanks – Lynn Sep 29 '20 at 14:58