Solved

Problem with Listing Outlook Contacts

Posted on 2014-02-12
1
417 Views
Last Modified: 2014-04-23
Hi
I'm trying to list some info for Outlook contacts - I want to see how many entries are in the OtherTelephoneNumber field. I'm using Outlook 2003.
My code is below. For the first few users, all worked well.
But for the most recent users, its crashing out on the "For Each olItem In searchFolder" line.
The error message is:
Debug: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.ContactItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063021-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

So there are some contacts that must be causing an issue. I really only want Contacts with an OtherTelephoneNumber - is there a way to make the searchFolder only have contacts that have an OtherTelephoneNumber?


Dim stUser As String = ""
      Dim iOtherCount As Integer = 0
      Dim iSearch As Integer = 0
      Dim stStage As String = ""

      Try

         'Start Outlook.
         ' If it is already running, you'll use the same instance...
         Dim olApp As outlook.Application
         olApp = CreateObject("Outlook.Application")

         ' Logon. Doesn't hurt if you are already running and logged on...
         Dim olNs As outlook.NameSpace
         olNs = olApp.GetNamespace("MAPI")
         olNs.Logon()

         Dim folderContacts As outlook.MAPIFolder = olNs.GetDefaultFolder(outlook.OlDefaultFolders.olFolderContacts)
         Dim searchFolder As outlook.Items = folderContacts.Items
         Dim olItem As outlook.ContactItem
         Dim Updated As String = ""
         Dim counter As Integer = 0

         For Each olItem In searchFolder
            If olItem.Class = outlook.OlObjectClass.olContact Then
               stUser = olItem.FullName
               If Not (olItem.OtherTelephoneNumber = Nothing) Then
                  iOtherCount += 1
                  Add_Text(olItem.FullName & " - " & olItem.OtherTelephoneNumber, False)
               End If
               iSearch += 1
            End If
         Next

         MsgBox("Complete: " & counter & "   Searched: " & iSearch)

      Catch ex As Exception
         MsgBox(stStage & vbCrLf & ex.Message, MsgBoxStyle.Critical, "User Update: " & stUser & " Other Count = " & iOtherCount & " Searched: " & iSearch)
         Add_Text(ex.Message, True)
      End Try

Open in new window

0
Comment
Question by:jdhackett
1 Comment
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39853664
I would suggest that you change your loop from For Each (because it is currently trying to convert items at that stage) to a For i loop. Example below


        Dim objOutlook As Outlook._Application
        objOutlook = New Outlook.Application
        Dim objNS As Outlook._NameSpace = objOutlook.Session
        Dim objContacts As Outlook.MAPIFolder = _
            objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
        Dim objItems As Outlook._Items = objContacts.Items
        Dim iCount As Int16 = objItems.Count
        Dim i As Int16
        For i = 1 To iCount
            If TypeOf (objItems.Item(i)) Is Outlook.ContactItem Then
                Console.WriteLine(objItems.Item(i).Fullname)
            End If
        Next

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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 …

746 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

13 Experts available now in Live!

Get 1:1 Help Now