Solved

Problem with Listing Outlook Contacts

Posted on 2014-02-12
1
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
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: …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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