How do I iterate through a collection of ContactItems?

G

Guest

I'm trying to iterate though ContactItems in a public folder and am
inconsistently getting the error message "Specified cast is not valid" in the
following code (the For Each loop):

subFolder = outlookNameSpace.Folders("Public Folders").Folders("All Public
Folders").Folders("Contacts")
strCriteria = "[MessageClass]='IPM.Contact'"
myItems = subFolder.Items.Restrict(strCriteria)

For Each item As Outlook.ContactItem In myItems
Debug.WriteLine(item.FirstName)
Next

The folder contains only ContactItems and still falls over even when I add a
filter of TypeName(item) = "ContactItem".

It doesn't always crash at the same point during the iteration which leads
me to suspect that it has something to do with the synchronisation of the
public folder.

Is there any way to 'lock' a current subset of the contacts to use for this
iteration?

I'm writing this in VB.NET with Outlook 2003 and SBS 2003.

Many Thanks
Jens
 
G

Guest

I am connecting to the Exchange server via RPC. What is the connection limit?
How do you release a connection?

Any help would be greatly appreciated.
 
G

Guest

I've managed to resolve this issue by using the ReleaseComObject method on
each object in the loop.

Jens said:
I am connecting to the Exchange server via RPC. What is the connection limit?
How do you release a connection?

Any help would be greatly appreciated.

Jens said:
I'm trying to iterate though ContactItems in a public folder and am
inconsistently getting the error message "Specified cast is not valid" in the
following code (the For Each loop):

subFolder = outlookNameSpace.Folders("Public Folders").Folders("All Public
Folders").Folders("Contacts")
strCriteria = "[MessageClass]='IPM.Contact'"
myItems = subFolder.Items.Restrict(strCriteria)

For Each item As Outlook.ContactItem In myItems
Debug.WriteLine(item.FirstName)
Next

The folder contains only ContactItems and still falls over even when I add a
filter of TypeName(item) = "ContactItem".

It doesn't always crash at the same point during the iteration which leads
me to suspect that it has something to do with the synchronisation of the
public folder.

Is there any way to 'lock' a current subset of the contacts to use for this
iteration?

I'm writing this in VB.NET with Outlook 2003 and SBS 2003.

Many Thanks
Jens
 

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