'How to Search Phone number in Address book?

There are options to get a contact detail from Mail id or Contact's Name using

Namespace.CreateRecipient / Recipient.Resolve

Is there any option to perform a direct search on Outlook Addressbook with Phone number or Company name etc.?

I am able to get many VBA coding options to loop through each contact in addressbook & find matching contact for phone number.



Solution 1:[1]

Please, try the next function. It should return the account name, searching by a phone number.

I played some minutes with my Outlook settings, because it used to not return any Address Book, even if it has a custom one from where it suggests the accounts address when start typing in the To box. If a similar problem in your case, I will try explaining what I did. But you maybe are luckier:

Function nameByPhoneNo(strPhNo As String) As String
 Dim olApp As Outlook.Application, olNS As Outlook.NameSpace
 Dim olGAL As Outlook.AddressList, olAdLs As Outlook.AddressLists
 Dim olEntry As Outlook.AddressEntries, olMember As Outlook.AddressEntry

 Set olApp = Outlook.Application
 Set olNS = olApp.GetNamespace("MAPI")
 Set olAdLs = olNS.AddressLists

 Set olGAL = olAdLs.item(1) 'Your "Contacts" address book must be the first!
                            'the code can be adapted to search in all of them, if many...
 If olGAL.AddressEntries.count < 1 Then
    MsgBox "You must set ""Contacts"" folder as ""Outlook Address Book"", from its ""Properties""."
    nameByPhoneNo = "No Address Book coould be found..."
    Exit Function
 End If

 Set olEntry = olGAL.AddressEntries

 For Each olMember In olEntry
    If InStr(olMember.GetContact.BusinessTelephoneNumber, strPhNo) > 0 Or _
       InStr(olMember.GetContact.HomeTelephoneNumber, strPhNo) > 0 Or _
       InStr(olMember.GetContact.MobileTelephoneNumber, strPhNo) > 0 Then
           nameByPhoneNo = olMember.GetContact.fullName: Exit Function
    End If
 Next
End Function

The function can be adapted to also search by "Company Name" etc.

It can be tested in this way:

Sub testNameByPhoneNo()
    Debug.Print nameByPhoneNo("123252900") 'use it as string (between double quotes)
End Sub

Solution 2:[2]

Unlike message store providers, most address book providers do not support arbitrary searches (only because Outlook does not use them), so on the low level most of them support simple PR_ANR searches, which is what CreateRecipient / Resolve use AFAIK - it is essentially "here is a string, find the best match that makes sense to you".

Some address book providers (such as GAL) expose search templates (you can see its UI in the Outlook address book if you click "Advanced Search". That functionality is available either in Extended MAPI (C++ or Delphi only) or in Redemption (I am its author - any language - see RDOAddressListSearch object). Unfortunately, phone number is not one of the supported search fields for GAL - only fist/last names, department, company, city, etc. are.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2