This post was inspired by a stackoverflow – VBA Search in Outlook question.
Here is an example that let’s you do a wildcard search using the Items Restrict Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | Option Explicit Sub Search_Inbox() Dim myOlApp As New Outlook.Application Dim objNamespace As Outlook.NameSpace Dim objFolder As Outlook.MAPIFolder Dim filteredItems As Outlook.Items Dim itm As Object Dim Found As Boolean Dim strFilter As String Set objNamespace = myOlApp.GetNamespace("MAPI") Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox) strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%sketch%'" Set filteredItems = objFolder.Items.Restrict(strFilter) If filteredItems.Count = 0 Then Debug.Print "No emails found" Found = False Else Found = True ' this loop is optional, it displays the list of emails by subject. For Each itm In filteredItems Debug.Print itm.Subject Next End If 'If the subject isn't found: If Not Found Then 'NoResults.Show Else Debug.Print "Found " & filteredItems.Count & " items." End If 'myOlApp.Quit Set myOlApp = Nothing End Sub |