Outlook distribution list hanging up on contact items query
outlook, vba
Solution
In order to distinguish between Lists and Contacts, you can alter your code to the following:
Sub ContactName()
On Error GoTo ErrHandler
Dim ContactsFolder As Folder
Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts)
MsgBox ("Contacts found: " & ContactsFolder.Items.Count)
Dim Contact As ContactItem
Dim distList As DistListItem
Dim i As Integer
For i = 1 To ContactsFolder.Items.Count
If TypeOf ContactsFolder.Items(i) Is DistListItem Then
Set distList = ContactsFolder.Items(i)
Debug.Print distList.DLName
ElseIf TypeOf ContactsFolder.Items(i) Is ContactItem Then
Set contact = ContactsFolder.Items(i)
Debug.Print contact.FullName
Else
Debug.Print "Item is something else"
End If
Next i
Exit Sub
ErrHandler:
Debug.Print Err.Description
Stop
Resume
End Sub
Note, I changed the property I'm accessing from CompanyName to FullName for my tests as I didn't have CompanyName for all my contacts.
Problem
I was working from some tutorial on MSDN to learn to make some macros for Outlook. I have this subroutine that gets hung up with a `Type mismatch` error. Upon stepping through error handling after `Stop` and `Resume` it goes back to `Next` and finishes the query. Looking through the result set in Immediate, one item is missing which is actually a distribution mailing list instead of a normal contact. I moved the mailing list out of my contacts for testing, and the error did not happen. I do plan on having other mailing lists as well, as this is for work. Is there a workaround like some way to escape it, other than just saving them somewhere else? Here is the code: ``` Sub ContactName() On Error GoTo ErrHandler Dim ContactsFolder As Folder Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts) MsgBox ("Contacts found: " & ContactsFolder.Items.Count) Dim Contact As ContactItem For Each Contact In ContactsFolder.Items Debug.Print Contact.CompanyName Next Exit Sub ErrHandler: Debug.Print Err.Description Stop Resume End Sub ```