Solved

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

Posted on 2006-10-27
2
541 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
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Create high volume marketing opportunities using email signatures with these top 10 DOs and DON'Ts of email signature marketing.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

11 Experts available now in Live!

Get 1:1 Help Now