
Managing many distribution lists might be difficult especially when their content is often changed. The most common problem is that when a contact is removed in Outlook, it is not automatically removed from the distribution lists.
A macro below finds a given e-mail address in all distribution lists from the current folder. After the search is finished the macro shows in what distribution lists the contact was found.
Sub SearchInDistLists()
On Error Resume Next
Dim oFolder As MAPIFolder
oFolder = Application.ActiveExplorer.CurrentFolder
' Ask for SMTP address to search
Dim strSearchAddress As String
strSearchAddress = InputBox("Please enter SMTP address to search in distribution lists in the folder.")
If InStr(strSearchAddress, "@") < 1 Then
MsgBox("Wrong SMTP")
Exit Sub
End If
Dim strDistListNames As String
For Each item In oFolder.Items
Dim oDistList As DistListItem
oDistList = item
If Not oDistList Is Nothing Then
For nIndex = 1 To oDistList.MemberCount
If UCase(oDistList.GetMember(nIndex).Address) = UCase(strSearchAddress) Then
strDistListNames = strDistListNames & oDistList.DLName & vbCrLf
Exit For ' address found
End If
Next
End If
Next
' Show results
If strDistListNames <> "" Then
MsgBox "Address '" & strSearchAddress & "' found in the following distribution lists:" & vbCrLf & vbCrLf & strDistListNames, vbInformation
Else
MsgBox "Address '" & strSearchAddress & "' not found in any distribution lists in the folder.", vbInformation
End If
End Sub© All rights reserved. No part or whole of this article may not be reproduced or published without prior permission.
Comments
pedroos 2011-12-09 00:43:28
The code had some additional symbols causing the syntax error - it should be ok now.
The code had some additional symbols causing the syntax error - it should be ok now.
Pedroos 2011-12-09 01:04:59
I forgot to ask - give us a shout if the script works ok please. Thanks!
I forgot to ask - give us a shout if the script works ok please. Thanks!
Kevin Zhang 2011-12-11 12:21:08
Hi there,
The search returns - "Address "abc@abc.com" not found in any distribution lists in thefolder.64" but it is in one of the distribution list, any idea?
Sub SearchEmail()
On Error Resume Next
Dim oFolder As MAPIFolder
oFolder = Application.ActiveExplorer.CurrentFolder
' Ask for SMTP address to search
Dim strSearchAddress As String
strSearchAddress = InputBox("Please enter SMTP address to search in distribution lists in the folder.")
If InStr(strSearchAddress, "@") < 1 Then
MsgBox ("Wrong SMTP")
Exit Sub
End If
Dim strDistListNames As String
For Each item In oFolder.Items
Dim oDistList As DistListItem
oDistList = item
If Not oDistList Is Nothing Then
For nIndex = 1 To oDistList.MemberCount
If UCase(oDistList.GetMember(nIndex).Address) = UCase(strSearchAddress) Then
strDistListNames = strDistListNames & oDistList.DLName & vbCrLf
Exit For ' address found
End If
Next
End If
Next
' Show results
If strDistListNames "" Then
MsgBox ("Address '" & strSearchAddress & "' found in the following distribution lists:" & vbCrLf & vbCrLf & strDistListNames & vbInformation)
Else
MsgBox ("Address '" & strSearchAddress & "' not found in any distribution lists in the folder." & vbInformation)
End If
End Sub
Hi there,
The search returns - "Address "abc@abc.com" not found in any distribution lists in thefolder.64" but it is in one of the distribution list, any idea?
Sub SearchEmail()
On Error Resume Next
Dim oFolder As MAPIFolder
oFolder = Application.ActiveExplorer.CurrentFolder
' Ask for SMTP address to search
Dim strSearchAddress As String
strSearchAddress = InputBox("Please enter SMTP address to search in distribution lists in the folder.")
If InStr(strSearchAddress, "@") < 1 Then
MsgBox ("Wrong SMTP")
Exit Sub
End If
Dim strDistListNames As String
For Each item In oFolder.Items
Dim oDistList As DistListItem
oDistList = item
If Not oDistList Is Nothing Then
For nIndex = 1 To oDistList.MemberCount
If UCase(oDistList.GetMember(nIndex).Address) = UCase(strSearchAddress) Then
strDistListNames = strDistListNames & oDistList.DLName & vbCrLf
Exit For ' address found
End If
Next
End If
Next
' Show results
If strDistListNames "" Then
MsgBox ("Address '" & strSearchAddress & "' found in the following distribution lists:" & vbCrLf & vbCrLf & strDistListNames & vbInformation)
Else
MsgBox ("Address '" & strSearchAddress & "' not found in any distribution lists in the folder." & vbInformation)
End If
End Sub
Anthony 2012-03-21 14:36:07
Here's code that is working:
Sub SearchInDistLists()
On Error Resume Next
'Dim oFolder As MAPIFolder
Dim oFolderItems As Items
Set oFolderItems = Application.ActiveExplorer.CurrentFolder.Items
' Ask for SMTP address to search
Dim strSearchAddress As String
strSearchAddress = InputBox("Please enter SMTP address to search in distribution lists in the folder.")
If InStr(strSearchAddress, "@") < 1 Then
MsgBox ("Wrong SMTP")
Exit Sub
End If
Dim strDistListNames As String
For Each Item In oFolderItems
Dim oDistList As DistListItem
Set oDistList = Item
If Not oDistList Is Nothing Then
If Not oDistList = Empty Then
For nIndex = 1 To oDistList.MemberCount
If UCase(oDistList.GetMember(nIndex).Address) = UCase(strSearchAddress) Then
strDistListNames = strDistListNames & oDistList.DLName & vbCrLf
Exit For ' address found
End If
Next
End If
End If
Next
' Show results
If strDistListNames "" Then
MsgBox "Address '" & strSearchAddress & "' found in the following distribution lists:" & vbCrLf & vbCrLf & strDistListNames, vbInformation
Else
MsgBox "Address '" & strSearchAddress & "' not found in any distribution lists in the folder.", vbInformation
End If
End Sub
Here's code that is working:
Sub SearchInDistLists()
On Error Resume Next
'Dim oFolder As MAPIFolder
Dim oFolderItems As Items
Set oFolderItems = Application.ActiveExplorer.CurrentFolder.Items
' Ask for SMTP address to search
Dim strSearchAddress As String
strSearchAddress = InputBox("Please enter SMTP address to search in distribution lists in the folder.")
If InStr(strSearchAddress, "@") < 1 Then
MsgBox ("Wrong SMTP")
Exit Sub
End If
Dim strDistListNames As String
For Each Item In oFolderItems
Dim oDistList As DistListItem
Set oDistList = Item
If Not oDistList Is Nothing Then
If Not oDistList = Empty Then
For nIndex = 1 To oDistList.MemberCount
If UCase(oDistList.GetMember(nIndex).Address) = UCase(strSearchAddress) Then
strDistListNames = strDistListNames & oDistList.DLName & vbCrLf
Exit For ' address found
End If
Next
End If
End If
Next
' Show results
If strDistListNames "" Then
MsgBox "Address '" & strSearchAddress & "' found in the following distribution lists:" & vbCrLf & vbCrLf & strDistListNames, vbInformation
Else
MsgBox "Address '" & strSearchAddress & "' not found in any distribution lists in the folder.", vbInformation
End If
End Sub
Hi Michal,
copied your code but got such error
Compile error:
Syntax error
MsgBox("Address '" & strSearchAddress & "' found in the following distribution lists:" & _
vbCrLf & vbCrLf & strDistListNames, vbInformation)
Else
MsgBox("Address '" & strSearchAddress & "' not found in any distribution lists in the folder.", vbInformation)
End If
Please advise, thanks.
Kevin Zhang