Outlook mailing list hanging on request of contact items

I worked from some MSDN tutorial to learn how to make some macros for Outlook. I have this routine that hangs with Type mismatch error. After performing error handling after Stop and Resume it returns to Next and completes the request.

Looking through the result set in Immediate, one element is missing, which is actually a distribution mailing list instead of a regular contact. I moved the mailing list from my contacts for testing, and the error did not occur.

I also plan on having other mailing lists as this is for work. Is there any workaround, like some way to avoid this, other than just saving them elsewhere?

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 
+6
source share
2 answers

To distinguish between lists and contacts, you can change 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 

Notice that I changed the property that I am accessing from CompanyName to FullName for my tests, because I did not have the CompanyName name for all my contacts.

+3
source
 Dim Contact As ContactItem For Each Contact In ContactsFolder.Items Debug.Print Contact.CompanyName Next 

When you define Contact as ContactItem , you tell VBA what exactly it should find in Items . This only works if all the elements in ContactFolder are actually ContactItems.

In other words, you specifically look at all the items in the bag, but you specifically make each of them Apple, but when you encounter Irange, VBA gives an error message.

What I usually do in this situation, instead of saying that I want to go through all the apples in the bag, I want to go through each item in the bag, so that something like:

 Dim mContact 'by not dimensioning it you basically allow mContact to become whatever type each item is For Each mContact In ContactsFolder.Items Debug.Print mContact.CompanyName Next 

Please note that I changed your name to mContact , because Contact is a likely keyword in VBA, and sometimes it's better not to deal with this.

This above will still cause errors, because any mContact that does not have a .CompanyName attribute.

What you can do is the following:

 Dim mContact For Each mContact In ContactsFolder.Items if mContact.Class = olContact then Debug.Print mContact.CompanyName Else Debug.Print "Not a Contact! Actually a :" & typename(mContact) End if Next 

This checks if the object you are repeating (the fruit from the bag) is actually an β€œapple”, and if not, tell what type of fruit it has.

+4
source

Source: https://habr.com/ru/post/978488/


All Articles