Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

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

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
Androminos
Asked:
Androminos
1 Solution
 
David LeeCommented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now