Question

Starting with the example at http://msdn.microsoft.com/en-us/library/office/aa209973(v=office.11).aspx, I was able to create the following block of code that searches the Outlook inbox for emails where the subject line starts with a specific phone number '555-5555' using "ci_startswith".

Public blnSearchComp As Boolean

Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
    MsgBox "The AdvancedSearchComplete Event fired."
    blnSearchComp = True
End Sub

Sub TestAdvancedSearchComplete()
    Dim sch As Outlook.Search
    Dim rsts As Outlook.Results
    Dim i As Integer
    blnSearchComp = False
    Const strF As String = "urn:schemas:mailheader:subject ci_startswith '555-5555'"
    Const strS As String = "Inbox"   
    Set sch = Application.AdvancedSearch(strS, strF) 
    While blnSearchComp = False
        DoEvents
    Wend 
    Set rsts = sch.Results
    For i = 1 To rsts.Count
        MsgBox rsts.Item(i).SenderName
    Next
End Sub

Obviously, if the subject line does not start with the exact phone number, in this case '555-5555', the search doesn't find the email. In place of "ci_startswith", I tried using the "like" comparison, however this now fails to find any matches, including those found with the "ci_startswith".

    Const strF As String = "urn:schemas:mailheader:subject like '555-5555'"

Am I using the "like" comparison incorrectly? From what I've read, it appears it should work. Or is this a known bug/issue? If so, are there any workarounds that provide a broader search ability?

Ultimately, I'd like to use something like below to be able to search for all possible instances of a phone number.

    Const strF As String = "urn:schemas:mailheader:subject like '###-####'"

Thanks in advance!

--------- EDIT / ADD ---------

Public blnSearchComp As Boolean

Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As search)
    MsgBox "The AdvancedSearchComplete Event fired."
    blnSearchComp = True
End Sub

Sub TestAdvancedSearchComplete()
    Dim sch As Outlook.search
    Dim rsts As Outlook.Results
    Dim i As Integer
    blnSearchComp = False
    Const strF As String = "urn:schemas:mailheader:subject LIKE '%###%'"
    Const strS As String = "Inbox"
    Set sch = Application.AdvancedSearch(strS, strF)
    While blnSearchComp = False
        DoEvents
    Wend
    Set rsts = sch.Results
    For i = 1 To rsts.Count
        MsgBox rsts.Item(i).SenderName
    Next
End Sub
Was it helpful?

Solution

when using "LIKE" you have to add the % character around your criteria depending on how you want to search

http://msdn.microsoft.com/en-us/library/office/cc513841(v=office.12).aspx

The keyword like performs prefix, substring, or equivalence matching. Tokens (characters, word, or words) are enclosed with the % character in a specific way depending on the type of matching: like '%' provides prefix matching. For example, restricting for like 'cat%' would match "cat" and "catalog." like '%%' provides substring matching. For example, restricting for like '%cat%' would match "cat," "catalog," "kittycat," and "decathlon." like '' provides equivalence matching. For example, restricting for like 'cat' would match "cat" and "RE: Cat."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top