Find a contact by telephone number

  • Thread starter Thread starter Angus Comber
  • Start date Start date
A

Angus Comber

Hello

In Outlook I can use the Find Items feature to find a contact.

Eg I can look for 01222 & 456789

How can I do the same using VB?

Angus
 
You can use the Restrict method. Here's an example built from the one in
the Help file:

Public Sub ContactPhoneNumber()
Dim myOlApp As Outlook.Application
Dim myNamespace As Outlook.NameSpace
Dim myContacts As Outlook.Items
Dim myItems As Outlook.Items
Dim myItem As Object
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myContacts = myNamespace.GetDefaultFolder(olFolderContacts).Items
Set myItems = myContacts.Restrict("[BusinessTelephoneNumber] = '+1 (555)
555-1234'")
For Each myItem In myItems
If (myItem.Class = olContact) Then
Debug.Print myItem.FullName
End If
Next
End Sub
 
Thanks that's really helpful. However, the problem is most people enter a
telephone number with spaces. In UK a London telephone number might be 020
8888 9999

I get from a phone system 022088889999

Any ideas on how I can do a generic search which eg ignores spaces. Or
ideally where I can ignore spaces, brackets, hyphens?

Angus

Eric Legault said:
You can use the Restrict method. Here's an example built from the one in
the Help file:

Public Sub ContactPhoneNumber()
Dim myOlApp As Outlook.Application
Dim myNamespace As Outlook.NameSpace
Dim myContacts As Outlook.Items
Dim myItems As Outlook.Items
Dim myItem As Object
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myContacts = myNamespace.GetDefaultFolder(olFolderContacts).Items
Set myItems = myContacts.Restrict("[BusinessTelephoneNumber] = '+1 (555)
555-1234'")
For Each myItem In myItems
If (myItem.Class = olContact) Then
Debug.Print myItem.FullName
End If
Next
End Sub

--
Eric Legault (Outlook MVP, MCDBA, old school WOSA MCSD, B.A.)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/


Angus Comber said:
Hello

In Outlook I can use the Find Items feature to find a contact.

Eg I can look for 01222 & 456789

How can I do the same using VB?

Angus
 
You can't use wildcards in the search, so you'll have to perform multiple
searches for each variation of known phone number formats. A pain, I know.

--
Eric Legault (Outlook MVP, MCDBA, old school WOSA MCSD, B.A.)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/


Angus Comber said:
Thanks that's really helpful. However, the problem is most people enter a
telephone number with spaces. In UK a London telephone number might be 020
8888 9999

I get from a phone system 022088889999

Any ideas on how I can do a generic search which eg ignores spaces. Or
ideally where I can ignore spaces, brackets, hyphens?

Angus

Eric Legault said:
You can use the Restrict method. Here's an example built from the one in
the Help file:

Public Sub ContactPhoneNumber()
Dim myOlApp As Outlook.Application
Dim myNamespace As Outlook.NameSpace
Dim myContacts As Outlook.Items
Dim myItems As Outlook.Items
Dim myItem As Object
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myContacts = myNamespace.GetDefaultFolder(olFolderContacts).Items
Set myItems = myContacts.Restrict("[BusinessTelephoneNumber] = '+1 (555)
555-1234'")
For Each myItem In myItems
If (myItem.Class = olContact) Then
Debug.Print myItem.FullName
End If
Next
End Sub

--
Eric Legault (Outlook MVP, MCDBA, old school WOSA MCSD, B.A.)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/


Angus Comber said:
Hello

In Outlook I can use the Find Items feature to find a contact.

Eg I can look for 01222 & 456789

How can I do the same using VB?

Angus
 
Back
Top