AJDeveloper
asked on
How to filter according to distribution list using items.find via outlook VBA
I'm trying to filter the contact folder to search for a specific DL .The problem is when I search usisng ContactFolder.Items[Folder Name] I'm getting an exception if the DL doesn't exist.
Im trying another syntax ,ContactFodler.Items.Find[ filter] but don't know which filter criteria I can use for DL ,can anybody help here ?
Thanks
Im trying another syntax ,ContactFodler.Items.Find[
Thanks
ASKER
Hi Chris
I'm checking if the recipient is a DL so that I can retrieve its email addresses :
object DLst = SharedInfo.ContactFodler.I tems[recip ient.Addre ssEntry.Na me]
this would throw an exception if the DL doesn't exist .
So m trying to filter the items according to the DL using the code:
SharedInfo.ContactFodler.I tems.Find( Filter)
My issue is I don't know what word criteria I should use to filter the items because according to the MSDN link :
http://msdn.microsoft.com/en-us/library/office/ff869662.aspx
I we try to search via DLName it would give error .
Thanks
I'm checking if the recipient is a DL so that I can retrieve its email addresses :
object DLst = SharedInfo.ContactFodler.I
this would throw an exception if the DL doesn't exist .
So m trying to filter the items according to the DL using the code:
SharedInfo.ContactFodler.I
My issue is I don't know what word criteria I should use to filter the items because according to the MSDN link :
http://msdn.microsoft.com/en-us/library/office/ff869662.aspx
I we try to search via DLName it would give error .
Thanks
What version of Outlook are you using?
ASKER
Outlook 2013
Thx...stand by
Do you want the code that searches for the distribution list to return a distribution list object?
ASKER
Hi Chris
Yes ,that's what I want but please don't give me code to loop the items in the contact folder because I already tried that and because I have many thousands of contacts ,outlook would hang when it loops
Thanks
Yes ,that's what I want but please don't give me code to loop the items in the contact folder because I already tried that and because I have many thousands of contacts ,outlook would hang when it loops
Thanks
I will give you that code first, because I think the following code will be fast.
(I could be wrong but give it a go)
If it is still slow, I will investigate down another path.
This code has a stub calling a function. If the Distribution list is found
a message box will appear and the code will stop.
Try itwith a valid distribution box name and then with a non-existant one
(to test how fast copde runs in the latter case).
(I could be wrong but give it a go)
If it is still slow, I will investigate down another path.
This code has a stub calling a function. If the Distribution list is found
a message box will appear and the code will stop.
Try itwith a valid distribution box name and then with a non-existant one
(to test how fast copde runs in the latter case).
Private Sub main1()
Dim MyDL As Object
Set MyDL = FindDL("Students List") 'change this to required name
If Not MyDL Is Nothing Then
' do something with the DL
'..........
'..........
Else
MsgBox "DL named Students List not found!" 'change message as applicable
End If
End Sub
Public Function FindDL(strDL As String) As Object
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder 'use Outlook.MAPIFolder if Outlook.folder fails.
Dim myFolderItems As Outlook.Items
Dim x As Integer
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderContacts)
Set myFolderItems = myFolder.Items
iCount = myFolderItems.Count
For x = 1 To iCount
If TypeName(myFolderItems.Item(x)) = "DistListItem" Then
If myFolderItems.Item(x).DLName = strDL Then
FindDL = myFolderItems.Item(x)
MsgBox "Distribution List "+strDL+ " found!"
Exit For
End If
End If
Next
End Function
OK, forget that last comment.
I think I have found the way to attack this.
We will use the RESTRICT method, but use it on the MessageClass property, rather than the prohibited (or should I say, "unuseable" ) DLNAME property.
The above code then becomes:
I think I have found the way to attack this.
We will use the RESTRICT method, but use it on the MessageClass property, rather than the prohibited (or should I say, "unuseable" ) DLNAME property.
The above code then becomes:
Private Sub main1()
Dim MyDL As Object
Dim strName as string
strDistName = "Student List" 'change to whatever you wish
Set MyDL = FindDL(strDistName)
If Not MyDL Is Nothing Then
' do something with the DL
'..........
'..........
Else
MsgBox "DL named "+ strDistName +" not found!"
End If
End Sub
Public Function FindDL(strDL As String) As Object
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder 'use Outlook.MAPIFolder if Outlook.folder fails.
Dim myDistList As Outlook.DistListItem
Dim myFolderItems As Outlook.Items
Dim MyRestrictItems As Outlook.Items
Dim sList As String
Dim x As Integer
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderContacts)
Set myFolderItems = myFolder.Items
Set MyRestrictItems = myFolderItems.Restrict("[MessageClass] = 'IPM.DistList'")
If MyRestrictItems.Count > 0 Then
For x = 1 To MyRestrictItems.Count
If MyRestrictItems(x).DLName = strDL Then
Set FindDL = MyRestrictItems(x)
MsgBox "Dist List for " + strDL + " found :-)" 'remove this after testing finished
Exit For
End If
Next
End If
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot Chris ,I will test it and update you
Any final comment on how things went?
You could iterate through all the items which are found by your filter search and teat to see whether the "type" of the found item is a DL.
After you supply your code I will see if I can tweak it to demonstrate.
Cheers
Chris