• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Filter and Save Contacts to a CSV File

Slipstick Systems

› Developer › Code Samples › Filter and Save Contacts to a CSV File

Last reviewed on April 27, 2015     4 Comments

A user at OutlookForums was looking for a macro to copy contacts to another folder for export:

I'm looking for a way to search my contact folder in Outlook for contacts in a certain category and ultimately export just those contacts into an excel workbook or CSV.

CSV example

If you don't need to do this often, create a view with the fields you need, filter by category then select all, copy and paste into Excel. See The No-Export way to use Outlook data in Excel for more details on this method.

Of course, if it's something you need to do frequently, it will be easier to use a macro and fully automate the process. I tweaked the CopyAppttoPrint macro to filter the contacts and put it together with Save selected messages to a single text file to filter and save the contacts as a CSV file.

The user will need to type the full category name into the dialog box, but using all lower case letters should work ok. Using partial names like "busin" for "business" won't work.

Type the category name

Note: when a birthdate is not entered, Outlook uses 1/1/4501, not "Null", so any contact with a blank birthdate will show 01 for the birthdate fields. Also, if you need the results wrapped in double quotes, use Chr(34) in the code.

This macro uses the FileScripting Object. You'll need to set a reference to Scripting Runtime in the VB Editor's Tools, References dialog.
Set a reference to scripting runtime

Sub SaveContactsinFile()
   
   Dim ContactsFolder As Outlook.Folder
   Dim ContactItems As Outlook.Items
   Dim ResItems As Outlook.Items
   Dim sFilter, strCategory As String
   Dim iNumRestricted As Integer
   Dim Item As ContactItem
   
   Dim objFS As New Scripting.FileSystemObject, objFile As Scripting.TextStream
   Dim strFile As String
   Dim sName As String

' Use the default folder
Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts)

' Use the selected folder
'Set ContactsFolder = Application.ActiveExplorer.CurrentFolder

' ===============
' Get file ready -
Dim enviro As String
enviro = CStr(Environ("USERPROFILE"))
 
' add the current date to the filename
sName = Format(Now(), "yyyymmdd")

 'random number avoids file exists error
   intHigh = 10000
   intLow = 1
 Randomize
   intNumber = Int((intHigh - intLow + 1) * Rnd + intLow)
 
' The folder path you use needs to exist
  strFile = enviro & "\Documents\" & sName & "-" & ContactsFolder & intNumber & ".csv"
   
  Set objFile = objFS.CreateTextFile(strFile, False)
  If objFile Is Nothing Then
    MsgBox "Error creating file '" & strFile & "'.", vbOKOnly + vbExclamation _
      , "Invalid File"
    Exit Sub
  End If
  
   With objFile
    .Write "email" & "," & "fname" & ","
    .Write "lname" & "," & "company" & ","
    .Write "telephone" & ","
    .Write "month" & "," & "day" & "," & "year"
    .Write vbCrLf
 End With
' ==================

   ' Get all of the items in the folder
  Set ContactItems = ContactsFolder.Items
   'create the Restrict filter
  strCategory = InputBox("Enter the category")

   sFilter = "[Categories] = " & strCategory
 
   ' Apply the filter to the collection
   Set ResItems = ContactItems.Restrict(sFilter)
   iNumRestricted = 0
   
   'Loop through the items in the collection.
  For Each Item In ResItems
      iNumRestricted = iNumRestricted + 1
      
  With objFile
    .Write Item.Email1Address & "," & Chr(34) & Item.FirstName & Chr(34) & ","
    .Write Chr(34) & Item.LastName & Chr(34) & "," & Chr(34) & Item.CompanyName & Chr(34) & ","
    .Write Chr(34) & Item.BusinessTelephoneNumber & Chr(34) & ","
    .Write Format(Item.Birthday, "mm") & "," & Format(Item.Birthday, "dd") & "," & Format(Item.Birthday, "yyyy")
    .Write vbCrLf
 End With
 
  Next
  objFile.Close
   
 ' MsgBox "Exported " & iNumRestricted & " contacts.", vbOKOnly + vbInformation, "DONE!"
   
  Set objFS = Nothing
  Set objFile = Nothing
  
   Set Item = Nothing
   Set ResItems = Nothing
   Set ContactItems = Nothing
   Set ContactsFolder = Nothing
   
End Sub
Filter and Save Contacts to a CSV File was last modified: April 27th, 2015 by Diane Poremsky
Post Views: 61

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Reddit (Opens in new window) Reddit
  • Share on Bluesky (Opens in new window) Bluesky
  • Share on Mastodon (Opens in new window) Mastodon
  • Email a link to a friend (Opens in new window) Email

Related Posts:

  • Delete Old Calendar Events using VBA
  • Save email message as text file
  • Assign a custom form to existing Outlook items
  • Add a Category to Contacts in a Contact Group

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Comments

  1. Victor50 says

    February 20, 2018 at 4:42 pm

    Update. I found

    "The Categories field is of type keywords, which is designed to hold multiple values. When accessing it programmatically, the Categories field behaves like a Text field, and the string must match exactly. Values in the text string are separated by a comma and a space. This typically means that you cannot use the Find and Restrict methods on a keywords field if it contains more than one value. For example, if you have one contact in the Business category and one contact in the Business and Social categories, you cannot easily use the Find and Restrict methods to retrieve all items that are in the Business category. Instead, you can loop through all contacts in the folder and use the Instr function to test whether the string "Business" is contained within the entire keywords field."

    Reply
    • Diane Poremsky says

      February 20, 2018 at 5:03 pm

      Correct... that was my first thought as to the problem, but when i tested it, it worked. 4 contacts - searched for blue category, found contacts had these categories:
      Blue
      Green, Blue
      Blue, Blue 2
      A 4th had only 'Blue 2' category and was not found, so it is filtering on the full categy name.

      Reply
  2. Victor50 says

    February 20, 2018 at 4:27 pm

    Hi, just tried you code. It only selects about half of the items in a category, can't find out why.

    Reply
    • Diane Poremsky says

      February 20, 2018 at 4:49 pm

      That usually means the count is messed up. Add this line right after inumrestricted = 0
      Debug.Print ResItems.Count
      look in the immediate window - is the count correct? does it match with the number exported?

      Is more than one category assigned to the missing items? (I cant repro missing items with more than one category or with categories with similar names)

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 31 Issue 10

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Deleting Auto-Complete Entries No Longer Works
  • Use Classic Outlook, not New Outlook
  • How to Remove the Primary Account from Outlook
  • How to Hide or Delete Outlook's Default Folders
  • Disable "Always ask before opening" Dialog
  • Removing Suggested Accounts in New Outlook
  • Change Outlook's Programmatic Access Options
  • Reset the New Outlook Profile
  • Adjusting Outlook's Zoom Setting in Email
  • Understanding Outlook's Calendar patchwork colors
  • Deleting Auto-Complete Entries No Longer Works
  • Sync Issues and Errors with Gmail and Yahoo accounts
  • Error Opening iCloud Appointments in Classic Outlook
  • Opt out of Microsoft 365 Companion Apps
  • Mail Templates in Outlook for Windows (and Web)
  • Urban legend: Microsoft Deletes Old Outlook.com Messages
  • Buttons in the New Message Notifications
  • Move Deleted Items to Another Folder Automatically
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Deleting Auto-Complete Entries No Longer Works

Sync Issues and Errors with Gmail and Yahoo accounts

Error Opening iCloud Appointments in Classic Outlook

Opt out of Microsoft 365 Companion Apps

Mail Templates in Outlook for Windows (and Web)

Urban legend: Microsoft Deletes Old Outlook.com Messages

Buttons in the New Message Notifications

Move Deleted Items to Another Folder Automatically

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2026 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.