> How can I get the list in order so it can show the emails by ReceivedTime descending.
You need to sort items in the descending order. For example, you do that in the code by using the following line:
```
Set myItems = MYFOLDER.Items
myItems.Sort "ReceivedTime", True
```
But later in the code the sorted collection is ignored:
```
For Each OLMAIL In MYFOLDER.Items
```
So, to get the right results you need to re-use the sorted collection for the loop:
```
For Each OLMAIL In myItems
```
> How can I add a lookback 45 days to the macro script?
Use the `Restrict` or `Find`/`FindNext` methods of the `Items` class. The following search criteria can be used to get items for a date range specified:
```
Dim datStartUTC As Date
Dim datEndUTC As Date
datStartUTC = oPA.LocalTimeToUTC(Date)
datEndUTC = oPA.LocalTimeToUTC(DateAdd("d", 45, Date))
'This filter uses urn:schemas:httpmail namespace
strFilter = AddQuotes("urn:schemas:httpmail:datereceived") _
& " > '" & datStartUTC & "' AND " _
& AddQuotes("urn:schemas:httpmail:datereceived") _
& " < '" & datEndUTC & "'"
```
Read more about these methods in the following articles:
- [How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)][1]
- [How To: Use Restrict method to retrieve Outlook mail items from a folder][2]
[1]: https://www.add-in-express.com/creating-addins-blog/2011/09/20/outlook-find-findnext-retrieve-mail/
[2]: https://www.add-in-express.com/creating-addins-blog/2011/09/23/outlook-restrict-method-retrieve-mail/
Instead of iterating over all items in the folder:
```
For Each OLMAIL In myItems
```
You need to use the `Find`/`FindNext` or `Restrict` methods of the `Items` class instead. In that case you will iterate over the items that correspond to your search criteria. Read more about these methods in the following articles:
- [How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)][1]
- [How To: Use Restrict method to retrieve Outlook mail items from a folder][2]
For example, you could use logical AND operator to combine two conditions together:
```
Dim datStartUTC As Date
Dim datEndUTC As Date
datStartUTC = oPA.LocalTimeToUTC(Date)
datEndUTC = oPA.LocalTimeToUTC(DateAdd("d", 45, Date))
'This filter uses urn:schemas:httpmail namespace
strFilter = AddQuotes("urn:schemas:httpmail:datereceived") _
& " > '" & datStartUTC & "' AND " _
& AddQuotes("urn:schemas:httpmail:datereceived") _
& " < '" & datEndUTC & "'"
```
And here is the auxiliary function used in the sample code above:
```
Public Function AddQuotes(ByVal SchemaName As String) As String
On Error Resume Next
AddQuotes = Chr(34) & SchemaName & Chr(34)
End Function
```
To make sure that the date-time comparison string is formatted as Microsoft Outlook expects, use the Visual Basic for Applications `Format` function (or its equivalent in your programming language).
[1]: https://www.add-in-express.com/creating-addins-blog/2011/09/20/outlook-find-findnext-retrieve-mail/
[2]: https://www.add-in-express.com/creating-addins-blog/2011/09/23/outlook-restrict-method-retrieve-mail/