Solved

Searching Outlook Contacts from VBA fails

Posted on 2009-04-05
2
236 Views
Last Modified: 2012-05-06
Attached is some code I am running to check for duplicate contacts in Outlook 2003 before inserting a new one from Access 2003. The adding part works fine so I have not included it here.
The problem is that the find method (Line 20) fails with the error:

The property "FirstName" in the condition is not valid.

Any example code I can find (even with this same property used in the search) seems to do the finding in exactly the same way as this, but mine won't work!
Any thoughts on what is wrong here?
Dim rst As New ADODB.Recordset

    Dim cnn As New ADODB.Connection

    Dim appOutlook As New Outlook.Application

    Dim ns As Outlook.Namespace

    Dim fldContacts As Outlook.MAPIFolder

    Dim itms As Outlook.Items, itm As Outlook.ContactItem

    Dim objContactFolder As Object

    Dim findstr As String

    

    Set cnn = CurrentProject.Connection

    Set appOutlook = CreateObject("Outlook.Application")

    Set ns = appOutlook.GetNamespace("MAPI")

    Set fldContacts = ns.GetDefaultFolder(olFolderContacts)

    Set itms = fldContacts.Items
 

    rst.Open "SELECT Employees.* FROM Employees WHERE Employees.EmployeeID=" & EmpID, cnn, adOpenKeyset, adLockOptimistic

    rst.MoveFirst

    

    'Prevent duplicate contacts in Outlook.

    findstr = "[FirstName] = """ & rst!FirstName & """ and [LastName] = """ & rst!LastName & """"

    Set itm = itms.Find(findstr)

    If Not TypeName(itm) = "Nothing" Then

      Rem dupicate

      If MsgBox("Do you want to update the information for " & rst!FirstName & " " & rst!LastName & "?", vbApplicationModal + vbYesNo + vbQuestion + vbDefaultButton1) = vbNo Then

          itm = Nothing

      End If

    End If

Open in new window

0
Comment
Question by:allaboutmike
2 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24074594
Nothing obviously wrong ... are you sure firstname and lastname are valid fields in your database record?

Chris
0
 

Accepted Solution

by:
allaboutmike earned 0 total points
ID: 24074644
Chris, thanks for the answer, but it seems that Outlook had some issue. I have gone back to get an exact dump of the error for you and now it works with no code changes!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now