Get email address of all users from all mails in Outlook Folder

Sometimes you want to send some important notice to everyone who has ever mailed you. Let's say you have a folder named "Friends" in Outlook where you store all the emails from your friends. Now you want to get all of their email addresses. Pretty difficult work if you have thousands of such mails. Here's an easy way.

  • Select the folder in Outlook and press ALT+F11. It will open Visual Basic Editor.
  • Double click on ThisOutlookSession from the Project tree.
  • Paste the following function:

Sub GetALLEmailAddresses()    

Dim objFolder As Folder
Set objFolder = Application.ActiveExplorer.Selection

Dim dic As New Dictionary
Dim strEmail As String
Dim strEmails As String

Dim objItem As MailItem
For Each objItem In objFolder.Items

strEmail = objItem.SenderEmailAddress
If Not dic.Exists(strEmail) Then
strEmails = strEmails + strEmail + ";"
dic.Add strEmail, ""
End If

Next

Debug.Print strEmails
End Sub

Hit F5 and it will run for a while. Then press Ctrl+G. You will see the email addresses in the "Immediate Window".    

Copy the whole string and you have all the email addresses from all the emails in the selected Outlook folder. There will be no duplicate address in the list.

Published Wednesday, August 09, 2006 1:55 PM by omar
Filed under:

Comments

# re: Get email address of all users from all mails in Outlook Folder

Wednesday, August 09, 2006 9:44 AM by Robert
More better...

Sub GetALLEmailAddresses()

Dim objFolder As MAPIFolder
Dim strEmail As String
Dim strEmails As String
''' Requires reference to Microsoft Scripting Runtime
Dim dic As New Dictionary
Dim objItem As Object

''Set objFolder = Application.ActiveExplorer.Selection
Set objFolder = Application.GetNamespace("Mapi").PickFolder

For Each objItem In objFolder.Items
   
   If objItem.Class = olMail Then
   
       strEmail = objItem.SenderEmailAddress

       If Not dic.Exists(strEmail) Then

           strEmails = strEmails + strEmail + ";"

           dic.Add strEmail, ""

       End If

   End If
   
Next

Debug.Print strEmails

End Sub

# re: Get email address of all users from all mails in Outlook Folder

Wednesday, August 09, 2006 3:16 PM by JJ
Hey what do I need installed for the script to run? I'm getting a 'user-defined type not defined' compile error on the Folder type.  I have the Office 2003 Resource Kit and .NET Programmability Support / VB Scripting Support features installed for Outlook.

# re: Get email address of all users from all mails in Outlook Folder

Thursday, August 17, 2006 10:47 PM by omar
Which line throws the error?

# re: Get email address of all users from all mails in Outlook Folder

Friday, August 18, 2006 1:08 PM by Dinesh Dhamija
First of all, I like your blogs.
Secondly, The code you gave did not work for me, So I chnaged the code a little to make it work.

Here is the updated code.

Sub GetALLEmailAddresses()
   Dim objExplorer As Explorer
   Set objExplorer = Application.ActiveExplorer()
   
   Dim objFolder As MAPIFolder
   Set objFolder = objExplorer.CurrentFolder
   
   Dim dic As New Dictionary
   Dim strEmail As String
   Dim strEmails As String
   Dim objItem As MailItem
   
   For Each objItem In objFolder.Items
       strEmail = objItem.SenderEmailAddress
       
       If Not dic.Exists(strEmail) Then
           strEmails = strEmails + strEmail + ";"
           dic.Add strEmail, ""
       End If
   Next
   
   Debug.Print strEmails

End Sub


Leave a Comment

(required) 
(required) 
(optional)
(required)