Home > front end >  Find Outlook Email using Email ID or Extended Property with VBA
Find Outlook Email using Email ID or Extended Property with VBA

Time:01-12

With the MS Graph API, I can see an Outlook Email's ID and set custom properties (Single Value extended property - https://learn.microsoft.com/en-us/graph/api/singlevaluelegacyextendedproperty-post-singlevalueextendedproperties?view=graph-rest-1.0).

Is there a way to find a specific email by searching on that EmailID or custom property with VBA? It doesn't seem like I can search those properties with 'Folder.Items.Restrict()'

Also, once I get an email item, I'm able to see the custom property I set through MS Graph Api.

oItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/string/{10101010-1010-1010-C101-1010101010101010}/myCustomProp")

But is there a way to query the inbox and find that specific email with VBA?

I tried Using Restrict() and PropertyAccessor but they don't seem to address my issue.

CodePudding user response:

You can use Items.Find/Restrict to search for a property using its DASL name. Use a query like

@SQL="http://schemas.microsoft.com/mapi/string/{10101010-1010-1010-C101-1010101010101010}/myCustomProp" = 'some value'

Note that the DASL name is quoted, so in VBA it will be

set item = SomeFolder.Items.Find("@SQL=""http://schemas.microsoft.com/mapi/string/{10101010-1010-1010-C101-1010101010101010}/myCustomProp"" = 'some value'")

CodePudding user response:

Binary properties (like EntryID and etc.) are not supported for searching items in Outlook. The best what you could do is to search items with a custom property. For example, you would use the following filter to search for a custom property named Mom's "Gift" that contains the word pearls:

filter = "@SQL=" & Chr(34) & _
    "https://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/" _
    & "Mom's "Gift"" & Chr(34) & " like '%pearls%'"

Note, we have used the Chr(34) function solve the problem with double quotes. Read more about string comparisons in the Filtering Items Using a String Comparison article.

To search for items that correspond to your conditions in an Outlook folder you can use the Find/FindNext or Restrict methods of the Items class. You can read more about them in the articles that I wrote for the technical blog:

But if you need to search for items in multiple folders or subfolders you may consider using the AdvancedSearch method of the Application class instead. The key benefits of using the AdvancedSearch method in Outlook are:

  • The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
  • Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
  • Full support for DASL queries (custom properties can be used for searching too). To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
  • You can stop the search process at any moment using the Stop method of the Search class.

See Advanced search in Outlook programmatically: C#, VB.NET for more information.

  • Related