Solved

Why Doesn't "FindNext" work with Outlook Object Library Contacts?

Posted on 2006-10-27
2
560 Views
Last Modified: 2007-12-19
Hey Folks,

I've got a VB6 App which I am using to manipulate Outlook 2003 contacts.  The problem is, the "Findnext" method doesn't seem to work as advertised in the context I am using it.  No error messages appear...but the contact object I am setting always comes up "Nothing" even though I know there's plenty of other contacts meeting the criteria of the original "Find" method (which DOES work).

Here's the section of code where it doesn't work--see asterisks for actual line that's not working:

  Dim ol As Object
  Dim olns As outlook.NameSpace
  Dim objFolder As outlook.MAPIFolder
  Dim Contact As outlook.ContactItem

  Set ol = New outlook.Application
  Set olns = ol.GetNamespace("MAPI")
  Set objFolder = olns.GetDefaultFolder(olFolderContacts)
 
  'Loop through Outlook subscribed contacts (where OrganizationalID = 2)
  Set Contact = objFolder.Items.Find("[OrganizationalIDNumber]='2'") 'This works just great
  If Contact Is Nothing Then
      blnStop = True
  Else
      If Not RS1 Is Nothing Then
        If RS1.State = adStateOpen Then
          RS1.Close
        End If
        Set RS1 = Nothing
      End If
      Set RS1 = New ADODB.Recordset
      With RS1
        .Fields.Append "FullName", adVarChar, 100, adFldIsNullable
        .Fields.Append "CompanyName", adVarChar, 100, adFldIsNullable
        .Fields.Append "GUID", adGUID, 50, adFldIsNullable
        .Fields.Append "Active", adBoolean, 1, adFldIsNullable
      End With 'rs1
  End If
  RS1.Open
  Do While Not blnStop
        With RS1
          .AddNew
          .Fields("FullName").Value = Trim(Contact.FullName)
          .Fields("CompanyName").Value = Trim(Contact.CompanyName)
          .Fields("GUID").Value = Trim(Contact.GovernmentIDNumber)
          .Fields("Active").Value = True
        End With 'rs1
'**********************************************************************************************
      'THIS IS WHERE THE PROBLEM LIES.  BY ALL ACCOUNTS, THIS SHOULD WORK BUT DOESN'T
      Set Contact = objFolder.Items.FindNext
      'No error is returned, but the Contact is always nothing after running the line above, where it should equal the next contact where the organizationalidnumber = 2 based on the "Find" above.
'**********************************************************************************************

      If Contact Is Nothing Then
        blnStop = True
      End If
  Loop

Please help!  And please don't just send a link to Microsoft's code explaining the "Findnext" method using appointments as examples...I've been all over that.
Also...just to cover things already tried:

I have double & triple-checked that there are indeed correct values in additional contacts (i.e. contacts where the OrganizationalIDNumber = "2").  I have tried Dimming the ol as an Outlook.application instead of an object.  I have tried placing () after the FindNext (i.e.:  ".FindNext()" )   All to no avail.  Thanks!!

-A
0
Comment
Question by:Androminos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 76

Accepted Solution

by:
David Lee earned 125 total points
ID: 17822210
Hi Androminos,

> objFolder.Items.Find
That's the problem.  For this to work Find needs to be working directly against an Items collection.  Change the declaration of objFolder from

    Dim objFolder As outlook.MAPIFolder

to

    Dim objFolder As outlook.Items

Then change these three lines respectively

    Set objFolder = olns.GetDefaultFolder(olFolderContacts)
    Set Contact = objFolder.Items.Find("[OrganizationalIDNumber]='2'")
    Set Contact = objFolder.Items.FindNext

to

    Set objFolder = olns.GetDefaultFolder(olFolderContacts).Items
    Set Contact = objFolder.Find("[OrganizationalIDNumber]='2'")
    Set Contact = objFolder.FindNext

That should get this working.  I tested this using the code below and it worked fine.

Sub TestingFind()
    Dim olkContact As Outlook.ContactItem, _
        olkItems As Outlook.Items
    Set olkItems = Session.GetDefaultFolder(olFolderContacts).Items
    Set olkContact = olkItems.Find("[OrganizationalIDNumber] = '2'")
    Do While TypeName(olkContact) <> "Nothing"
        Debug.Print olkContact.FullName
        Set olkContact = olkItems.FindNext
    Loop
    Set olkContact = Nothing
    Debug.Print "Done!"
End Sub


Cheers!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

627 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