Forum Discussion

Winderson's avatar
Winderson
Copper Contributor
Apr 25, 2025

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 by date. However, I need more advanced filtering options — for example, checking if the subject contains a specific word.

I could use a loop to do this after retrieving the results with Restrict, but it's very slow. It seems like AdvancedSearch would be a better option, but I'm getting an error when I try to run the search.

Below is an example of my code, although I've tried many variations. If anyone can share a working example, that would be really helpful. Thanks in advance!

string searchTag = "BuscaOutlook";
string searchScope = "'\\Caixa de Entrada'";
filter = "@SQL=\"urn:schemas:httpmail:subject\" LIKE '%orcamento%' AND \"urn:schemas:httpmail:datereceived\" >= '2024-04-01 00:00:00'";
try
{
    dynamic searchb = outlookApp.AdvancedSearch(searchScope, filter, true, searchTag);
}
catch (Exception ex)
{
    MessageBox.Show("Erro no AdvancedSearch:\n" + ex.Message);
    return;
}

 

1 Reply

  • bhadreshpatel's avatar
    bhadreshpatel
    Copper 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.

Resources