Solved

Problem with Listing Outlook Contacts

Posted on 2014-02-12
1
420 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Find out what you should include to make the best professional email signature for your organization.
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
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 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: …

828 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