For Each DistListItem and Runtime Error '13'

Z

Zarqy

I'm new to Outlook vb porgramming, and have a simple question

In the code below, I am attempting to list the distribution lists in my
personal contacts folder.

The first example does the initial loop, but will not continue. Why? I
would prefer to not have to iterate over every single contact if
possible in order to speed things up.

(My eventual goal is to loop thru my contacts, and match them up with
what dl's they are in. Do I need to do the same loop inside itself to
do this? Probably best for a seperate post...)

'--- Start Example ---
Dim obj As Object
Dim oDistList As Outlook.DistListItem
Dim oContact As Outlook.ContactItem
Dim oItems As Outlook.Items

Set oItems =
Application.GetNamespace("mapi").GetDefaultFolder(olFolderContacts).Items

' Obtain List of DL's

''--- Would prefer this method as it seems it would be a faster way
For Each oDistList In oItems
Debug.Print oDistList.DLName, oDistList.MemberCount
'--- Runtime Error '13' at this line (Next)
Next

'---This loop works fine
For Each obj In oItems
If TypeOf obj Is Outlook.DistListItem Then
Set oDistList = obj
Debug.Print oDistList.DLName, oDistList.MemberCount
End If
Next
 
K

Ken Slovak - [MVP - Outlook]

The loop is failing because it's hitting an item that's a contact and not a
DL.

If you want to iterate only DL's then get the Items collection of the folder
and set a Restrict clause on it to filter only items with the MessageClass
for a DL. That way the restricted Items collection you get returned to you
will only have DL's in it.

See the Object Browser help on Restrict for examples of how to set up a
restriction.
 
Z

Zarqy

Ken,

the Restrict look simple enough, but I am lost with the "MessageClass
for a DL" part.

in my example would I still use the oDistList?

For Each oDistList.Restrict("[???] = 'DistListItem' ") in oItems
...

Is this even close? I'm sure once I see this in action it will make
sense, but right now it is as clear as muddy water. I see something,
but not sure its the right thing.
 
Z

Zarqy

Also,

doesn't the
Dim oDistList As Outlook.DistListItem
....
For Each oDistList In oItems
already act as a filter?
Ken,

the Restrict look simple enough, but I am lost with the "MessageClass
for a DL" part.

in my example would I still use the oDistList?

For Each oDistList.Restrict("[???] = 'DistListItem' ") in oItems
...

Is this even close? I'm sure once I see this in action it will make
sense, but right now it is as clear as muddy water. I see something,
but not sure its the right thing.

The loop is failing because it's hitting an item that's a contact and not a
DL.

If you want to iterate only DL's then get the Items collection of the folder
and set a Restrict clause on it to filter only items with the MessageClass
for a DL. That way the restricted Items collection you get returned to you
will only have DL's in it.

See the Object Browser help on Restrict for examples of how to set up a
restriction.
 
K

Ken Slovak - [MVP - Outlook]

Declaring the object as a DL only early binds it to that type of object. If
you try to assign a contact to a DL object you get an error. No filtering is
involved in declarations.

The MessageClass for a DL is "IPM.DistList", so your restriction filter
would look like this:

strFilter = "[MessageClass] = 'IPM.DistList'"

Dim colFiltered As Outlook.Items

Set colFiltered = oItems.Restrict(strFilter)
For Each oDistList In colFiltered
'blah, blah
Next




Zarqy said:
Also,

doesn't the
Dim oDistList As Outlook.DistListItem
...
For Each oDistList In oItems
already act as a filter?
Ken,

the Restrict look simple enough, but I am lost with the "MessageClass
for a DL" part.

in my example would I still use the oDistList?

For Each oDistList.Restrict("[???] = 'DistListItem' ") in oItems
...

Is this even close? I'm sure once I see this in action it will make
sense, but right now it is as clear as muddy water. I see something,
but not sure its the right thing.
 
Z

Zarqy

Ken,

THANK YOU! That filter has sped up my code incredibly! I no longer have
to loop thru every contact just to check the dl's members.

I've been programming in VB for years, and for some reason the
early/late binding concept has always been on the fringe of making
sense to me. Your explanaition is another step in my comprehension.
Declaring the object as a DL only early binds it to that type of object. If
you try to assign a contact to a DL object you get an error. No filtering is
involved in declarations.

The MessageClass for a DL is "IPM.DistList", so your restriction filter
would look like this:

strFilter = "[MessageClass] = 'IPM.DistList'"

Dim colFiltered As Outlook.Items

Set colFiltered = oItems.Restrict(strFilter)
For Each oDistList In colFiltered
'blah, blah
Next




Zarqy said:
Also,

doesn't the
Dim oDistList As Outlook.DistListItem
...
For Each oDistList In oItems
already act as a filter?
Ken,

the Restrict look simple enough, but I am lost with the "MessageClass
for a DL" part.

in my example would I still use the oDistList?

For Each oDistList.Restrict("[???] = 'DistListItem' ") in oItems
...

Is this even close? I'm sure once I see this in action it will make
sense, but right now it is as clear as muddy water. I see something,
but not sure its the right thing.
 

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