Solved

Problem with Listing Outlook Contacts

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
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…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

929 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

16 Experts available now in Live!

Get 1:1 Help Now