Home > Net >  Outlook vba Search contact name using partial info
Outlook vba Search contact name using partial info

Time:07-14

Outlook vba

I'm using this script to search contact and get its email. Using Outlook local contact folder (now its name is People I think).

Public Sub ShowContactEmail()

Dim myContacts As Folder
Dim myfilter As String
Dim filteredItems As Outlook.Items
Dim myName As String
myName = "John Doe"

Set myContacts = Application.Session.GetDefaultFolder(olFolderContacts)
Set filteredItems = myContacts.Items

myfilter = "[FirstName] = " & myName & " Or [LastName] = " & myName & " Or [FullName] = " & myName

For Each myItem In filteredItems.Restrict(myfilter)
    If (myItem.Class = olContact) Then
    Debug.Print myItem.Email1Address
    End If
Next
 
End Sub

It works BUT only if name is exactly as in contact folder. If I look for "Doe" won't find anything.

How to do a partial search? Try to use "like" and "wildcarts" here but not success. VBA noob. Also not sure if I have to search in FirstName, Lastname or FullName. This PC is used by several people and not always input new contact details as I want.

Thanks

CodePudding user response:

If I understand microsoft docs correctly you can use char(32) as a 'wildcard' in your search

CodePudding user response:

When matching string properties, you can use either a pair of single quotes ('), or a pair of double quotes ("), to delimit a string that is part of the filter. Typically in VBA Chr(34) function is used which stands for double quotes. For example:

myfilter = "[FirstName] = " & Chr(34) & myName & Chr(34) & " Or [LastName] = " & Chr(34) & myName & Chr(34) & " Or [FullName] = " & Chr(34) & myName & Chr(34)

Read more about that in the Filtering Items Using a String Comparison article.

CodePudding user response:

If you are just trying to resolve a name, try Application.Session.CreateRecipient("John Doe") / Recipient.Resolve.

  • Related