Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird Type Mismatch Error 1

Status
Not open for further replies.

Jables

Programmer
Aug 28, 2001
148
US
I am working with this little subroutine that will enumerate certain properties of items in an Outlook Contact Folder, but it's throwing a type mismatch on a certain iteration of a For...Next loop everytime, and I have no idea why. See comments. Here's the subroutine:

Code:
Sub EnumFolders()
'Taken From pg 644 of Programming MS Access 2002 -Rick Dobson
'Will run as a module from within Outlook itself or Access

    Dim myOlApp As Outlook.Application
    Dim myNameSpace As NameSpace
    Dim myFolders As Outlook.Folders
    Dim myFolder As Outlook.MAPIFolder
    Dim myContact As Outlook.ContactItem
    Dim i As Integer
    
    Set myOlApp = CreateObject("Outlook.Application")
    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    Set myFolders = myNameSpace.Folders.Item(1).Folders
    i = 0
    
    'Enumerate folders
    For Each myFolder In myFolders
        Debug.Print myFolder.Parent, myFolder.Name, _
            myFolder.Items.Count
            If myFolder.Name = "Master Contacts" Then
                'I was looking to enumerate items in the Master Contacts Folder
                'That's what's happening here.
                For Each myContact In myFolder.Items
                    'Routine throws a type mismatch on the 69th iteration of this loop.
                    Debug.Print i & "--> " & myContact.FileAs
                    i = i + 1
                Next
            End If
    Next
    
    Set myOlApp = Nothing

End Sub
 
It seems as if you're running into something that's not a ContactItem. Contact folders can contain address lists. Could you be running into an AddressList item?

You may need to use a generic Object variable in the For Each, and use TypeOf inside the loop to determine whether it's actually a ContactItem. If so, you can set myContact from the generic object variable.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top