Skip to content

Instantly share code, notes, and snippets.

@KiNgMaR
Created August 28, 2013 20:57
Show Gist options
  • Save KiNgMaR/6371170 to your computer and use it in GitHub Desktop.
Save KiNgMaR/6371170 to your computer and use it in GitHub Desktop.
This VBA script for MS Outlook counts all unique *recipient* addresses from emails in the current inbox and writes them to a text file.
' This VBA script for MS Outlook counts all unique recipient addresses
' from emails in the current inbox and writes them to a text file.
Option Explicit
' REQUIRED: Add Project Reference to "Microsoft Scripting Library" for Dictionary class!
Sub ListReceivedMailAddrs()
Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oInbox As Outlook.MAPIFolder
Dim oMsg As Outlook.MailItem
Dim oRecip As Outlook.Recipient
Dim oResultDict As Dictionary
Dim oFSO As FileSystemObject
Dim oTextFile As TextStream
Dim i As Integer
Dim sAddr As String
' CREATE OBJECTS
Set oApp = CreateObject("Outlook.Application")
Set oNS = oApp.GetNamespace("mapi")
Set oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Set oResultDict = New Dictionary
' READ INDIVIDUAL ADDRESSES
For Each oMsg In oInbox.Items.Restrict("[MessageClass]='IPM.Note'")
For Each oRecip In oMsg.Recipients
sAddr = LCase(oRecip.Address)
If Not oResultDict.Exists(sAddr) Then
oResultDict(sAddr) = 1
Else
oResultDict(sAddr) = oResultDict(sAddr) + 1
End If
Next
Next
' WRITE TO TEXT FILE
Set oFSO = New FileSystemObject
Set oTextFile = oFSO.CreateTextFile("C:\temp\addresses.txt", True)
For i = 0 To oResultDict.Count - 1
oTextFile.WriteLine oResultDict.Items(i) & vbTab & oResultDict.Keys(i)
Next
oTextFile.Close
MsgBox "All done!", vbInformation
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment