Forum Discussion
Winderson
Apr 25, 2025Copper Contributor
Using late binding to run AdvancedSearch in Outlook in .NET8?
I'm trying to use the AdvancedSearch feature from Outlook in my .NET 8 application, but I haven't been able to make it work. I'm currently using Restrict when I need to filter by a specific sender or...
bhadreshpatel
May 01, 2025Copper Contributor
I was facing a similar issue with AdvancedSearch in Outlook from a .NET app, and after a lot of trial and error, I finally got it working. The main thing I was doing wrong was using the wrong filter format - I was trying SQL-like syntax with LIKE, but it turns out you need to use DASL syntax.
Here’s a basic example that worked for me:
string searchTag = "BuscaOutlook";
string searchScope = "'Inbox'"; // Make sure the folder name matches exactly (case/language-sensitive)
string filter =
"urn:schemas:httpmail:subject LIKE '%orcamento%' AND " +
"urn:schemas:httpmail:datereceived >= '2024-04-01T00:00:00Z'";
try
{
var searchResult = outlookApp.AdvancedSearch(searchScope, filter, true, searchTag);
// Handle results in the AdvancedSearchComplete event
outlookApp.AdvancedSearchComplete += (Search s) =>
{
var results = s.Results;
foreach (dynamic item in results)
{
// Process your mail item here
}
};
}
catch (Exception ex)
{
MessageBox.Show("Erro no AdvancedSearch:\n" + ex.Message);
}
A couple of tips:
Make sure Outlook is running.
Use the exact folder name (in my case, I had to switch from '\\Caixa de Entrada' to just 'Inbox').
The filter must follow DASL format, not SQL.