I'm attempting to implement a VBA Module within Outlook (v2038) that will allow you to search your mailbox using the Internet Message ID of a message. I've stumbled my way across the internet on how to accomplish this, and found two promising methods; but they are failing to return results when tested with present message's internet message IDs. I'm hoping to get someone with expertise on the matter to aid me in this.
Module 1: (This will allow you select a specific folder/subfolder and search by msgID within Outlook)
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
If SearchObject.Tag = "MessageId" Then
Set Results = SearchObject.Results
MsgBox "Message-ID search complete. " & Results.Count & " result(s) found."
For i = 1 To Results.Count
Results.Item(i).Display
Next
End If
End Sub
Public Sub SearchMessageId()
Set Folder = Session.PickFolder
If Not Folder Is Nothing Then
r = MsgBox("Include subfolders?", vbYesNoCancel, "Search by Message-ID")
If r <> vbCancel Then
MessageId = InputBox("Message-ID:")
If MessageId <> "" Then
Application.AdvancedSearch "'" & Folder.FolderPath & "'", "http://schemas.microsoft.com/mapi/proptag/0x1035001F = '" & MessageId & "'", r = vbYes, "MessageId"
End If
End If
End If
End Sub
Module 2: (This will search your Default Folder on Outlook by msgID within VBA)
Sub UseDASLFilters()
Dim ol As Outlook.Application
Dim ns As Outlook.NameSpace
Dim fol As Outlook.Folder
Dim i As Object
Dim mi As Outlook.MailItem
Dim filterString As String
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set fol = ns.GetDefaultFolder(olFolderInbox)
filterString = "@SQL=""http://schemas.microsoft.com/mapi/proptag/0x1035001F"" = '<[email protected]>' "
For Each i In fol.Items.Restrict(filterString)
If i.Class = olMail Then
Set mi = i
Debug.Print mi.SenderEmailAddress, mi.Subject, mi.ReceivedTime
End If
Next i
End Sub
From the information I could gather, the MAPI property tag for PR_INTERNET_MESSAGE_ID should be 0x1035001F, 0x1035001E, or &H1035001E.
When using each proptag mentioned within Module2, no results were returned, so perhaps I need to define [Set i = Nothing] and/or [Dim '' as FileSystemObject] [Set '' = CreateObject()]...
(EDIT: MsgID must be closed with "<>")