We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

Androminos
Androminos asked
on
Medium Priority
643 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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010
Commented:
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!

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.