0

Hello all and thank you for your support,

Please, anyone knows how to get both cases where email is "something@emailprovider.com" and "Name Surname"?

I'm using the following filter

; email Search Parameters
lookForRecipient            := ReceiverEmail
strQuery                    := "urn:schemas:httpmail:displayto like '%" lookForRecipient "%'"
emailScopeFolder            := "\\myemail@outlook.com\inbox\myFolder\mysubFolder"
searchSubfolders            := True
searchObject := this.outlook.AdvancedSearch(emailScopeFolder, strQuery, searchSubfolders)

; Wait until Outlook's AdvancedSearch returns AdvancedSearchComplete
oSearchEvents           := new SearchEvents
ComObjConnect(this.outlook, oSearchEvents)
    while (!oSearchEvents.SearchComplete) {
        ToolTip, % "Wait. Search in progress"
    }   

; Get final results. Sort the results by time received time, selecting the newest (latest) one
searchResults := searchObject.Results
countResults := searchResults.Count()
searchResults.Sort("[ReceivedTime]", true)
latestSentEmail := searchResults.GetFirst()

Obs, elsewhere in the code, I have the class SearchEVents defined

class SearchEvents
{
    AdvancedSearchComplete(searchObject)
    {
        ToolTip, % "Wait. SearchEvents.AdvancedSearchComplete() is in progress"
        Sleep, 200
        this.SearchComplete := true
    }
}

And it works to find latest email sent. But not always.

If the email "ReceiverEmail" is something like "abc@hotmail.com"

This would be found enter image description here

But this would not be found enter image description here

So, using displayTo as namespace, it finds the email only if the email really appears in the To Field. If instead of the email there is a "name", the email is not found!

How can I search in "To" field?

Obs: I tried the options below without success.

enter image description here

Or, in text format,

;strQuery                    := "urn:schemas:httpmail:displayto like '%" lookForRecipient "%'" ; WORKS, but only if ToField is something@emailprovider.com
;strQuery                    := "urn:schemas:httpmail:to like '%" lookForRecipient "%'" ; no result
;strQuery                    := "urn:schemas:httpmail:namespace:to like '%" lookForRecipient "%'" ; no result
;strQuery                    := "urn:schemas:httpmail:reply-to like '%" lookForRecipient "%'" ; no result
;strQuery                    := "urn:schemas:httpmail:reply_to like '%" lookForRecipient "%'" ; no result
;strQuery                    := "urn:schemas:httpmail:reply_by like '%" lookForRecipient "%'" ; no result
;strQuery                    := "urn:schemas:httpmail:reply-by like '%" lookForRecipient "%'" ; error

;strQuery                    := "urn:schemas:mailheader:to like '%" lookForRecipient "%'" ; no result
;strQuery                    := "urn:schemas:mailheader:in-reply-to like '%" lookForRecipient "%'" ; no result
;strQuery                    := "urn:schemas:mailheader:reply-to like '%" lookForRecipient "%'" ; no result

strQuery                    := "urn:schemas:mailheader:return-receipt-to like '%" lookForRecipient "%'" ; no result

Please, anyone knows how to get both cases where email is "something@emailprovider.com" and "Name Surname"?

Thank you very much!

=============================== @niton

I have tried Items.Restrict(strFiler) as the link you sent without success.

subboxMail                  := 

this.mailBox.Folders(this.emailBox).Folders(this.emailSubBox)
    
strFilter                   := "@SQL=" Chr(34) "http://schemas.microsoft.com/mapi/proptag/0x0076001E" Chr(34) " like '%" lookForRecipient "%'"



restrictResults             := subboxMailItems.Restrict(strFilter)
                restrictResultsCount        := restrictResults.Count

                msgbox, % "searchedSubFolderCount = " searchedSubFolderCount "`n`n strFilter = " strFilter "`n`n restrictResultsCount = " restrictResultsCount

The message box returns searchSubfolderCount = 2000+ (the exact number of emails in the folder) but restrictResultCount = 0

enter image description here

Obs, and if I use two times the double quotes (") it raises an error. enter image description here

Eduardo GS
  • 33
  • 1
  • 7
  • 1
    Address-related searches are problematic. https://stackoverflow.com/a/64217427/1571407. "urn:schemas:httpmail:displayto" appears to be misunderstood or incorrectly implemented. https://stackoverflow.com/questions/63413285/search-by-email-address-for-latest-email-in-all-folders-and-reply-all#comment112132153_63413285 – niton Jan 13 '21 at 16:59
  • Niton, thanks for your reply. Tried your link (Items.Restrict(strFilter)) without success and added comment in the original post. – Eduardo GS Jan 13 '21 at 19:59
  • I am suggesting query implementation **may** not be correct in **VBA**. For example https://stackoverflow.com/questions/55924883/how-to-filter-an-outlook-view-in-vba-based-on-to-email-addresses. In a comment "...I get 2 different results, one from the dialog box, one in code, with supposedly the same DASL quesry!" – niton Jan 13 '21 at 20:20
  • Yes, that's exactly the same problem... Thanks, Niton! – Eduardo GS Jan 14 '21 at 11:09
  • Is it possible to retrieve AccountName from email? This way, we could search for displayTo = AccountName first retrieving AccountName from email... – Eduardo GS Jan 14 '21 at 11:55
  • If you cannot find a previously asked question create one. Leave this one open in case there is a solution. – niton Jan 14 '21 at 15:48
  • @niton I got it working("kind of") using a workaround. As I was not finding a solution, I found a workaround: write in tiny (on invisible color, same as background) letters "emailSentTo:xxx@yyy.com" at the body of the email. Then, use AdvancedSearch with argument "urn:schemas:httpmail:textDescription" like %emailSentTo:xxx@yyy.com". – Eduardo GS Jan 21 '21 at 20:20

0 Answers0