Search code examples
outlookinteropadvanced-search

Outlook Interop AdvancedSearch call fails with filter


I'm trying to run the AdvancedSearch function on the Outlook.Application class but I'm receiving an exception with the message "The operation has failed.". How do I get this to complete successfully?

I was previously using the basic search function with success to search a folder. I now have a need for my code to identify if the search produced any results. Therefore I require the advanced search function. When I call the function without a filter specified, outlook will successfully navigate to the folder. When I include the filter value an exception is raised. I'm not checking for it here but the IsInstantSearchEnabled property is true.

Dim scope = $"'{searchFolder.FolderPath}'"
Dim searchAttachment = "https://schemas.microsoft.com/mapi/proptag/0x0EA5001E"
Dim filter = $"@SQL={Chr(34)}{searchAttachment}{Chr(34)} ci_phrasematch '{searchText}'"
Dim search = app.AdvancedSearch(scope, filter)

scope: '\???\Inbox\???'

filter: @SQL="https://schemas.microsoft.com/mapi/proptag/0x0EA5001E" ci_phrasematch '???'

??? are just replaced values for this posting.


Solution

  • Apparently, the filter argument on AdvancedSearch needs the '@SQL=' portion omitted in order to function whereas other methods require it. Removing that portion of the string fixed it.