The Outlook object model is common for all programming languages, so I hope you will be able to apply the following explanation to a PS script.
There are several ways to get the job done it right in Outlook.
- Use the
Find
/FindNext
or Restrict
method of the Items class to get the items collection that correspond to your search criteria and then iterate over all of them. Read more about these methods in the following articles:
- Use the Table.Restrict method which applies a filter to the rows in the
Table
and obtains a new Table
object. The Filter
parameter is a query on specified properties of items that are represented as rows in the parent Table
. The query uses either the Microsoft Jet syntax or the DAV Searching and Locating (DASL) syntax. For example, the following Jet filter and DASL filter specify the same criteria for items with ReceivedTime
earlier than 3:30pm of December 12, 2021:
criteria = "[ReceivedTime] < '" & Format$("12/12/2021 3:30PM","General Date") & "'"
For example:
Sub RestrictTable()
'Declarations
Dim Filter As String
Dim oRow As Outlook.Row
Dim oTable As Outlook.Table
Dim oFolder As Outlook.Folder
'Get a Folder object for the Inbox
Set oFolder = Application.Session.GetDefaultFolder(olFolderSentItems)
'Define Filter to obtain items last modified after November 1, 2005
Filter = "[ReceivedTime] > '12/12/2021'"
'Restrict with Filter
Set oTable = oFolder.GetTable(Filter)
'Enumerate the table using test for EndOfTable
Do Until (oTable.EndOfTable)
Set oRow = oTable.GetNextRow()
Debug.Print (oRow("EntryID"))
Debug.Print (oRow("Subject"))
Debug.Print (oRow("CreationTime"))
Debug.Print (oRow("LastModificationTime"))
Debug.Print (oRow("MessageClass"))
Loop
End Sub