Get Email Addresses from outlook using VB.NET

I need to populate a combo box with email addresses in Microsoft Outlook.    I have found a couple of examples online but cant get any of them to work.   Here is one of the samples I found:

 Dim objOutlook As Microsoft.Office.Interop.Outlook._Application
        'Outlook Namespace will be current session
        Dim objNS As Microsoft.Office.Interop.Outlook._NameSpace

        'Initialise objects created in Form Load
        objOutlook = New Microsoft.Office.Interop.Outlook.Application()
        objNS = objOutlook.Session

        'Get the Contact folder
        Dim objAddressList As Microsoft.Office.Interop.Outlook.MAPIFolder
        objAddressList = objNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts)

        'Get all the contacts
        Dim objItems As Microsoft.Office.Interop.Outlook.Items = objAddressList.Items
            Dim objContact As Microsoft.Office.Interop.Outlook.contactitem

        'Loop through all contacts and add to combo box
        For j = 1 To objItems.Count
            objContact = objItems(j)
                Me.ComboBox1.Items.Add(objContact.Email1Address)
        Next

        'clear memory
        objContact = Nothing
        objItems = Nothing
            objAddressList = Nothing

When I run this I get the attached error and I don't know what to do to resolve it.   Any help would be appreciated.

error message
Mike_StevensAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pcelbaCommented:
On what line occurs this error?  
Dim objContact As Microsoft.Office.Interop.Outlook.contactitem  ?
objContact = objItems(j)  ?

What value is in the  objItems.Count ?

Could you post the value of objItems(1).Class ?
And also  objItems(1).Email1Address ?

Outlook installation can be damaged... Did you try the code on two different PCs?
Mike_StevensAuthor Commented:
For j = 1 To objItems.Count
                objContact = objItems(j)  <---------------- CAUSING THE ERROR
                Me.ComboBox1.Items.Add(objContact.Email1Address)
            Next
pcelbaCommented:
What about answers to other questions?

You could also try to remove the problematic line and assign the combo item directly:

Me.ComboBox1.Items.Add(objAddressList.Items(j).Email1Address)
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

CodeCruiserCommented:
Try changing your code to

  For j = 1 To objItems.Count
       If TypeOf(objContact) Is Microsoft.Office.Interop.Outlook.contactitem Then
            objContact = objItems(j)
                Me.ComboBox1.Items.Add(objContact.Email1Address)
       End If
  Next
pcelbaCommented:
Good observation. But it depends where the problem is...

I would rather try:

For j = 1 To objItems.Count
       If TypeOf(objItems(j)) Is Microsoft.Office.Interop.Outlook.contactitem Then
            objContact = objItems(j)
                Me.ComboBox1.Items.Add(objContact.Email1Address)
       End If
  Next

If contacts contain some unexpected object then above code should work.

Of course, I would like to know the value of  j  variable when the error occurs and also whether the code is successful for other  j  values.
Mike_StevensAuthor Commented:
I tried codecruiser's suggestion and the error did go away but it did not return any records.    Then I tried pcelba's suggestion which worked but only returned 6 records.
Mike_StevensAuthor Commented:
An update to my last comment:   Outlook on the development machine I am using only has 6 records in the outlook contacts and pcelba's code returned all six so I guess his code worked.
pcelbaCommented:
Great!

The question is on what Contacts folder entry it crashed before? Do you have any subfolders in your Outlook Contacts?
Mike_StevensAuthor Commented:
Im not sure what is considered a subfolder.  On the development machine I am working on I have contacts in "Outlook" and more under "iCloud",   I am guess they each are a sub folder.  After working with this some more I have found that am only ready from the "outlook" list and not that contact under "iCloud".  

You would be nice to be able to read them both.
pcelbaCommented:
Following line selects the Outlook contacts in your code:
 objAddressList = objNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts)

I would guess Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts = 10
(https://msdn.microsoft.com/en-us/library/office/ff861868.aspx)

So everything you need to process iCloud contacts is to find the right constant which you may do in a short loop. Or you may enumerate all Outlook Folders (incl. non-default ones) as described e.g here: https://msdn.microsoft.com/en-us/library/office/ff860950.aspx

Of course, it could also be possible the iCloud folder is not accessible this way. You have to try it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.