You need to find a specific email based on a value in the internet header, such as a custom tracking ID or a spam score. Outlook’s built-in search does not allow you to search directly inside internet header fields. This article explains how to create a custom property in Outlook that extracts a value from the internet header, then use that property to search and filter your items.
The method uses Outlook VBA to read the header text, parse the target value, and store it in a user-defined field. Once the custom property is populated, you can search it with Instant Search or create a search folder. This approach works for any header field, including X-Spam-Status, Received, Authentication-Results, or custom X-headers.
You will learn how to write the VBA macro, assign it to a button, and apply the custom property to your search criteria. No third-party add-ins are required.
Key Takeaways: Searching by Internet Header Value in Outlook
- VBA macro with PropertyAccessor.GetProperty: Reads the PR_TRANSPORT_MESSAGE_HEADERS property to access the full internet header.
- User-Defined Field in Folder: Stores the extracted header value as a custom text field searchable by Instant Search.
- Search Folder with custom property criteria: Automatically collects all items matching a specific header value without manual scanning.
How Internet Headers and Custom Properties Work in Outlook
Every email contains internet headers that record routing information, authentication results, and custom metadata added by mail servers. Outlook stores the complete header in the MAPI property PR_TRANSPORT_MESSAGE_HEADERS (DASL name: http://schemas.microsoft.com/mapi/proptag/0x007D001E). This property is not exposed in the default Outlook interface.
A custom property, also called a user-defined field, is a text field you add to a folder. You can write data into it using VBA and then search that field with Instant Search queries such as custom_field_name:value. The field lives in the folder schema and persists until you remove it. Each item in the folder can have a different value for the same custom field.
To search by header value, you need a macro that does three things:
- Read the full header of a selected email.
- Parse the target header line and extract the value.
- Write that value into a custom property on the same item.
Once the custom property is populated, you can search across the folder using that property as a criterion. This method works only on items you have already processed with the macro. For new incoming emails, you can run the macro automatically using the ItemAdd event.
Steps to Create a Custom Property and Search by Internet Header Value
Follow these steps to set up the VBA macro, create the custom field, and run the extraction on your emails.
- Enable the Developer Tab
Open Outlook. Go to File > Options > Customize Ribbon. In the right column, check the box for Developer and click OK. The Developer tab appears in the ribbon. - Open the VBA Editor
Click the Developer tab, then click Visual Basic. Alternatively, press Alt+F11 on your keyboard. - Insert a New Module
In the VBA editor, go to Insert > Module. A blank code window opens. - Paste the Macro Code
Copy and paste the following code into the module window. ReplaceX-Custom-Headerwith the exact header name you want to search.Sub ExtractHeaderToCustomField() Dim olApp As Outlook.Application Dim olExp As Outlook.Explorer Dim olSel As Outlook.Selection Dim olMail As Outlook.MailItem Dim strHeader As String Dim strHeaderName As String Dim strValue As String Dim intPos As Integer Dim intEndPos As Integer Set olApp = Outlook.Application Set olExp = olApp.ActiveExplorer Set olSel = olExp.Selection ' Change this to the header you want to extract strHeaderName = "X-Custom-Header" If olSel.Count = 0 Then MsgBox "Select at least one email." Exit Sub End If For Each olMail In olSel ' Read the full internet header strHeader = olMail.PropertyAccessor.GetProperty( "http://schemas.microsoft.com/mapi/proptag/0x007D001E") ' Find the target header line intPos = InStr(1, strHeader, strHeaderName & ":", vbTextCompare) If intPos > 0 Then ' Get the start of the value (after ": ") intPos = intPos + Len(strHeaderName) + 2 intEndPos = InStr(intPos, strHeader, vbCrLf) If intEndPos = 0 Then intEndPos = Len(strHeader) + 1 strValue = Mid(strHeader, intPos, intEndPos - intPos) strValue = Trim(strValue) ' Write the value into a user-defined field olMail.UserProperties("HeaderValue") = strValue olMail.Save End If Next Set olMail = Nothing Set olSel = Nothing Set olExp = Nothing Set olApp = Nothing MsgBox "Done." End Sub - Add the Custom Field to the Folder
In Outlook, right-click the folder you want to search (for example, Inbox). Choose Properties. Go to the General tab and click the button labeled New under Fields available in this folder. Create a new text field namedHeaderValueand click OK twice. - Run the Macro on Selected Emails
In Outlook, select one or more emails. Press Alt+F8 to open the Macros dialog. ChooseExtractHeaderToCustomFieldand click Run. The macro reads each email’s header, extracts the value of the specified header, and writes it into the HeaderValue field. - Search Using the Custom Property
Click inside the Instant Search box at the top of the folder. TypeHeaderValue:yourvaluereplacingyourvaluewith the actual value you want to find. Outlook filters the list to show only items where the custom field matches.
Automate the Macro for New Emails
To run the extraction automatically when new mail arrives, use the Items.ItemAdd event in VBA. Open the VBA editor, double-click ThisOutlookSession in the Project Explorer, and paste the code below. Replace Inbox with your target folder name if needed.
Private WithEvents olInboxItems As Outlook.Items
Private Sub Application_Startup()
Dim olNS As Outlook.NameSpace
Set olNS = Application.GetNamespace("MAPI")
Set olInboxItems = olNS.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
Dim olMail As Outlook.MailItem
Set olMail = Item
' Call the extraction subroutine
Call ExtractHeaderToCustomFieldSingle(olMail)
End If
End Sub
Sub ExtractHeaderToCustomFieldSingle(olMail As Outlook.MailItem)
Dim strHeader As String
Dim strHeaderName As String
Dim strValue As String
Dim intPos As Integer
Dim intEndPos As Integer
strHeaderName = "X-Custom-Header"
On Error Resume Next
strHeader = olMail.PropertyAccessor.GetProperty(
"http://schemas.microsoft.com/mapi/proptag/0x007D001E")
If Err.Number <> 0 Then Exit Sub
intPos = InStr(1, strHeader, strHeaderName & ":", vbTextCompare)
If intPos > 0 Then
intPos = intPos + Len(strHeaderName) + 2
intEndPos = InStr(intPos, strHeader, vbCrLf)
If intEndPos = 0 Then intEndPos = Len(strHeader) + 1
strValue = Mid(strHeader, intPos, intEndPos - intPos)
strValue = Trim(strValue)
olMail.UserProperties("HeaderValue") = strValue
olMail.Save
End If
End Sub
Common Issues and Limitations When Searching by Header Value
The macro returns an error on some emails
Some emails, such as meeting requests, do not have the PR_TRANSPORT_MESSAGE_HEADERS property. Add an On Error Resume Next statement before the GetProperty call to skip those items gracefully. The macro above already includes this for the automated version.
The custom field does not appear in search results
Instant Search indexes only fields that exist in the folder schema. Verify that the field name is exactly HeaderValue and that you added it to the correct folder. If the folder is in a search scope that excludes custom fields, use the Search Tools > Search Options > Indexing Options to rebuild the index.
The header value contains extra spaces or line breaks
Headers can wrap across multiple lines. The macro above extracts only the first line of the header value. For multi-line values, modify the loop to concatenate lines that start with a space or tab. A robust parser would read the entire header block between the header name and the next header.
Search does not find the value after running the macro
The custom property is populated only on items processed by the macro. Items that were not selected or not in the target folder will not have the field. Run the macro on all existing items in the folder, or use the automated ItemAdd event for future mail.
Instant Search vs Search Folder: Comparison of Methods
| Item | Instant Search Query | Search Folder with Custom Property |
|---|---|---|
| Setup effort | None after macro runs | Create a search folder once |
| Real-time updates | No, must re-run macro for new items | Yes if macro runs on ItemAdd |
| Query syntax | HeaderValue:value |
Advanced Find with custom field criteria |
| Persistent view | Temporary until you clear search | Folder stays until deleted |
| Works with indexing | Yes, if field is indexed | Yes, same index |
You can now extract any internet header value into a custom Outlook field and search your items using that value. The VBA macro gives you full control over which header to use and which folder to target. For recurring searches, set up a search folder with the HeaderValue field as the criteria and enable the ItemAdd automation to keep it current.
To extend this method, modify the macro to extract multiple header values into separate custom fields. You can also export the values to an Excel sheet using Outlook VBA and the Range.Value property for further analysis. The PropertyAccessor object can read many hidden MAPI properties beyond headers, such as message ID or reply-to address.