?
Solved

Problem with Listing Outlook Contacts

Posted on 2014-02-12
1
Medium Priority
?
431 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Mailbox Overload?
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
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 …
Suggested Courses

752 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