Problem Looping Through Contacts

A

Andibevan

Hi,

I am trying to loop through all the contacts in the standard contacts
folder.

When I use "Dim objContact As ContactItem" it gets 15 items through the 600
contact items and gets a 'type mismatch' error on the 'Next Statement'

When I change it to 'Dim objContact' It prints out all items as expected.
If I print the typename for each of the 600 objContact items it says
ContactItem for each item.

Any ideas?

Tia

Andy


Sub SearchAndReplaceContacts()
On Error Resume Next

'Const olFolderContacts = 10

'Set objOutlook = CreateObject("Outlook.Application")
Dim objNamespace As NameSpace
Set objNamespace = Application.GetNamespace("MAPI")

Dim colContacts As Items
Set colContacts = objNamespace.GetDefaultFolder(10).Items

'Dim objContact As ContactItem
Dim objContact

Dim i
For Each objContact In colContacts
Debug.Print i & " - " & objContact.FullName & " - " &
objContact.Email1Address _
& " - " & objContact.Email2Address _
& " - " & objContact.Email3Address
'Debug.Print i & TypeName(objContact)
i = i + 1
Next objContact
Debug.Print colContacts.Count & TypeName(objContact)


End Sub
 
E

Eric Legault [MVP - Outlook]

Logically, it's not a Contact item, whatever the TypeName says. To be safe,
stick with a generic Object, or inspect the Class or MessageClass properties
before assigning the objContact variable.
 
A

Andibevan

Hi Eric - Afraid I don't quite get what you mean by inspect the Class or
MessageClass properties before assigning the objContact variable

Bit confused as I presumed the objContact variable was assigned by the Next
statement and can't see how to inspect the 'next' item before it is
assigned.
 
E

Eric Legault [MVP - Outlook]

Try this approach to be safe in regards to the object you assume you'll be
working with:

Dim objItem As Object, objContact As Outlook.ContactItem

For Each objItem In colContacts
If objItem.Class = olContact Then
Set objContact = objItem
Debug.Print i & " - " & objContact.FullName & " - " &
objContact.Email1Address _
& " - " & objContact.Email2Address _
& " - " & objContact.Email3Address
End If
Set objItem = Nothing
Set objContact = Nothing
Next

--
Eric Legault (Outlook MVP, MCDBA, MCTS: Messaging & Collaboration)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top