Locate a specific contact in a subfolder using VBA

Can anyone help me on the VBA to find and extract information on a specific contact in a subfolder within Contacts in Outlook?  I have code that will loop through all contacts, and I can actually do a check on the name using a loop, as in, if myitem.CompanyName = "Coke" then, etc etc, but this takes a few seconds to loop through my list - there must be a quicker way.
LVL 2
mjsteele001Asked:
Who is Participating?
 
David LeeConnect With a Mentor Commented:
Hi, mjsteele001.

You can speed this up by using the Find or Restrict method of a folder's Items property rather than looping through all the items.  For example
Dim olkItem As Outlook.ContactItem
Set olkItem = olkFolder.Items.Find("[CompanyName] = 'Coke'")
If TypeName(olkItem) = "Nothing" Then
    'Code for whatever you want to do if no matching contacts were found'
Else
    'Code for whatever you want to do if a match was found'
End If

Open in new window

0
 
mjsteele001Author Commented:
Thanks - works great, except... how to assign the company name (coke) to a variable? - tried the usual but I can't get it to work!
0
 
mjsteele001Author Commented:
Sorted!
CompanyName = "[CompanyName] = '" & "Coke" & "'"
Dim olkItem As Outlook.ContactItem
Set olkItem = olkFolder.Items.Find(CompanyName)
If TypeName(olkItem) = "Nothing" Then
    'Code for whatever you want to do if no matching contacts were found'
Else
    'Code for whatever you want to do if a match was found'
End If

Open in new window

0
 
mjsteele001Author Commented:
Hello BlueDevilFan, thank you for your time.
0
 
David LeeCommented:
You're welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.