PDA

View Full Version : Contact Group causing error



PeterRHawkes
10-18-2011, 09:34 AM
The following code runs through each contact in the default folder. Problem is that when it comes across a Contact Group the 'Next olContact' statement creates a Type Mismatch with olContact = Nothing.

How can I prevent this error? Is it possible to filter out the Contact Groups? :banghead:

Set olApp = Outlook.Application
Set objName = olApp.GetNamespace("MAPI")
Set olFolder = objName.GetDefaultFolder(olFolderContacts)
Set olContact = olFolder.Items.GetFirst

For Each olContact In olFolder.Items

With olContact
' various actions on contact take place here
End With

Next olContact

Thank you.

Peter R Hawkes

steve_flash
10-20-2011, 09:01 AM
remember that folder your script is targeting must use OAB for the name resolution to work in distro lists
Try to set MAPIFolder.ShowAsOutlookAB property to true

JP2112
10-27-2011, 09:44 AM
As you've found, the Contacts folder can hold other items besides Contacts (DistListItems being one). Compare each item in the folder to the ContactItem type using TypeName or Type Of before attempting an operation on a ContactItem.


Dim olApp As Outlook.Application
Dim objName As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim obj As Object
Dim olContact As Outlook.ContactItem
Dim i As Long

Set olApp = Outlook.Application
Set objName = olApp.GetNamespace("MAPI")
Set olFolder = objName.GetDefaultFolder(olFolderContacts)

For i = 1 to olFolder.Items.Count
' grab each item
Set obj = olFolder.Items(i)
' is it a ContactItem?
If TypeName(obj) = "ContactItem" Then
' use a typecast variable and continue with operation
Set olContact = obj
With olContact
' various actions on contact take place here
End With
End If
Next i

PeterRHawkes
10-28-2011, 04:21 AM
Excellent, all now working as expected and personal KB increased!

Thank you.

Peter H