Skip to main content

How to export email addresses based on specific domain in Outlook?

Author: Xiaoyang Last Modified: 2022-10-19

If you want to export the email addresses with a specific domain from all contacts in your Outlook, please read this tutorial which will help you to apply a VBA code for extracting all the email addresses in a specific domain to a text file as below screenshot shown.


Export email addresses based on specific domain in Outlook with VBA code

To extract all email addresses with a specific domain from all contacts, please do with the following steps:

1. Create a new text file and give a name to it, see screenshot:

2. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

3. Then, click Insert > Module, and paste the following code in the Module Window.

VBA code: Export email addresses based on specific domain

Dim GDomain As String
Dim GFileSystem As Object
Dim GFilePath As String
Dim GFileObj As Object
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, _
  ByVal ipOperation As String, ByVal lpFile As String, _
  ByVal lpParameters As String, ByVal lpDirectory As String, _
  ByVal nShowCmd As Long) As Long

Sub ExportListOfEmailAddressesInSpecificDomain()
'Updateby ExtendOffice
Dim xStore As Store
Dim xFolder As Folder
On Error Resume Next
GDomain = InputBox("Enter domain(@***.com):", "Kutools for Outlook")
If Len(GDomain) <> 0 Then
  GFilePath = "C:\Users\skyyang\Desktop\Email Addresses with specific domain.txt" 'Specify the file path
  Set GFileSystem = CreateObject("Scripting.FileSystemObject")
  Set GFileObj = GFileSystem.CreateTextFile(GFilePath, True)
  For Each xStore In Application.Session.Stores
    For Each xFolder In xStore.GetRootFolder.Folders
      If xFolder.DefaultItemType = olContactItem Then
        Call ProcessFolders(xFolder)
      End If
    Next
  Next
  GFileObj.Close
  ShellExecute 0&, vbNullString, GFilePath, vbNullString, vbNullString, 1
End If
End Sub

Sub ProcessFolders(ByVal Fld As Outlook.Folder)
Dim xContactItems As Items
Dim I As Long
Dim xContact As ContactItem
Dim xSubFolder As Folder
On Error Resume Next
Set xContactItems = Fld.Items
For I = xContactItems.Count To 1 Step -1
  If xContactItems(I).Class = olContact Then
    Set xContact = xContactItems(I)
    If InStr(xContact.Email1Address, GDomain) > 0 Then
      GFileObj.WriteLine (xContact.Email1Address & vbCrLf)
    ElseIf InStr(xContact.Email2Address, GDomain) > 0 Then
      GFileObj.WriteLine (xContact.Email2Address & vbCrLf)
    ElseIf InStr(xContact.Email3Address, GDomain) > 0 Then
      GFileObj.WriteLine (xContact.Email3Address & vbCrLf)
    End If
  End If
Next
If Fld.Folders.Count > 0 Then
  For Each xSubFolder In Fld.Folders
    If xSubFolder.DefaultItemType = olContactItem Then
      Call ProcessFolders(xSubFolder)
    End If
  Next
End If
End Sub
Note: In the above code, change the file path "C:\Users\skyyang\Desktop\Email Addresses with specific domain.txt" to your own.

4. And then, press F5 key to run this code. A prompt box is popped out, please type the email domain that you want to export the email addresses based on, see screenshot:

5. Then, click OK button, and all the email addresses in the specific domain are extracted into the text file at once, see screenshot:


Best Office Productivity Tools

Breaking News: Kutools for Outlook Launches Free Version!

Experience the all-new Kutools for Outlook FREE version with 70+ incredible features, yours to use FOREVER! Click to download now!

πŸ€– Kutools AI : Instant pro emails with AI magic--one-click to genius replies, perfect tone, multilingual mastery. Transform emailing effortlessly! ...

πŸ“§ Email Automation: Auto Reply (Available for POP and IMAP)  /  Schedule Send Emails  /  Auto CC/BCC by Rules When Sending Email  /  Auto Forward (Advanced Rules)   /  Auto Add Greeting   /  Automatically Split Multi-Recipient Emails into Individual Messages ...

πŸ“¨ Email Management: Recall Emails  /  Block Scam Emails by Subjects and Others  /  Delete Duplicate Emails  /  Advanced Search  /  Consolidate Folders ...

πŸ“ Attachments ProBatch Save  /  Batch Detach  /  Batch Compress  /  Auto Save   /  Auto Detach  /  Auto Compress ...

🌟 Interface Magic: 😊More Pretty and Cool Emojis   /  Remind you when important emails come  /  Minimize Outlook Instead of Closing ...

πŸ‘ One-click Wonders: Reply All with Incoming Attachments  /   Anti-Phishing Emails  /  πŸ•˜Show Sender's Time Zone ...

πŸ‘©πŸΌβ€πŸ€β€πŸ‘©πŸ» Contacts & Calendar: Batch Add Contacts From Selected Emails  /  Split a Contact Group to Individual Groups  /  Remove Birthday Reminders ...

Instantly unlock Kutools for Outlook with a single clickβ€”permanently free. Don't wait, download now and boost your efficiency!

 

 

 

Comments (0)
No ratings yet. Be the first to rate!
There are no comments posted here yet
Leave your comments
Posting as Guest
Rate this post:
0   Characters
Suggested Locations