Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-10-27
2
Medium Priority
?
566 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 500 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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.
This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

719 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