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.
source share