Skip to main content

Kutools for Office — One Suite. Five Tools. Get More Done.

How to export outlook message counts to Excel workbook?

Author Xiaoyang Last modified

Normally, you can export the Outlook messages to Excel file by using the Import /Export feature quickly and easily. But, have you ever tried to count the items in all folders of your specific Email account and export the count results to Excel workbook?

Export outlook message counts to Excel workbook with VBA code


Export outlook message counts to Excel workbook with VBA code

The following VBA code can help you to export the count results from all folders in a specific Email account to Excel workbook, please do as this:

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

2. Click "Insert" > "Module", and paste the following code in the "Module" Window.

VBA code: Export outlook items count to Excel workbook

Sub Export_CountOfItems_InEachFolder_toExcel()
    Dim xSourceFolder As Outlook.Folder, xSubFolder As Outlook.Folder
   Dim xFilePath As String
    Dim xExcelApp As Excel.Application
    Dim xWb As Excel.Workbook
    Dim xWs As Excel.Worksheet
    On Error Resume Next
    Set xExcelApp = New Excel.Application
    Set xWb = xExcelApp.Workbooks.Add
    Set xWs = xWb.Sheets(1)
    xWs.Cells(1, 1) = "Folder"
    xWs.Cells(1, 2) = "Count Items"
    Set xSourceFolder = Outlook.Application.Session.PickFolder
    If xSourceFolder = nill Then
        xWb.Close False
        xExcelApp.Quit
        Exit Sub
    End If
    For Each xSubFolder In xSourceFolder.Folders
        Call ProcessFolders(xWs, xSubFolder)
    Next
    xWs.Columns("A:B").AutoFit
    Set xShell = CreateObject("Shell.Application")
    Set xFolder = xShell.BrowseforFolder(0, "Select a Folder:", 0, 0)
   If TypeName(xFolder) = "Nothing" Then
        xWb.Close False
        xExcelApp.Quit
        Exit Sub
    End If
    Set xFolderItem = xFolder.Self
    xFilePath = xFolderItem.Path & "\"
    xFilePath = xFilePath & xSourceFolder.Name & "(" & Format(Now, "yyyy-mm-dd hh-mm-ss") & ").xlsx"
    xWb.Close True, xFilePath
    xExcelApp.Quit
    Set xShell = Nothing
    MsgBox "Complete!", vbExclamation, "Kutools for Outlook"
End Sub
Sub ProcessFolders(ByVal Ws As Worksheet, ByVal xCurFolder As Outlook.Folder)
    Dim xSubFld As Folder
    Dim xItemCount As Long
   Dim xRow As Integer
    xItemCount = xCurFolder.Items.Count
    xRow = Ws.UsedRange.Rows.Count + 1
    Ws.Cells(xRow, 1) = xCurFolder.FolderPath
    Ws.Cells(xRow, 2) = xItemCount
    If xCurFolder.Folders.Count > 0 Then
       For Each xSubFld In xCurFolder.Folders
           Call ProcessFolders(Ws, xSubFld)
       Next
    End If
End Sub

3. And, still in the "Microsoft Visual Basic for Applications" window, click "Tools" > "References" to go to the "References-Project1" dialog box, and check "Microsoft Excel Object Library" option from the "Available References" list box, see screenshot:

doc export items count 1

4. Then click "OK", and then press "F5" key to run this code, a "Select Folder" is popped out, please select an Email account that you want to export the items count, see screenshot:

doc export items count 2

5. Then click "OK", and another "Browse For Folder" is displayed, please choose a folder to put the Excel file, see screenshot:

doc export items count 3

6. At last, click "OK" button, and the items count in all folder of the selected account has been exported to an Excel workbook, you can open the Excel file to view the result, see screenshot:

doc export items count 4

AI Mail Assistant in Outlook: Smarter Replies, Clearer Communication (one-click magic!) FREE

Streamline your daily Outlook tasks with the AI Mail Assistant from Kutools for Outlook. This powerful tool learns from your past emails to offer intelligent and accurate responses, optimize your email content, and help you draft and refine messages effortlessly.
doc ai email handle

This feature supports:

  • Smart Replies: Get responses crafted from your past conversations—tailored, precise, and ready to go.
  • Enhanced Content: Automatically refine your email text for clarity and impact.
  • Effortless Composition: Just provide keywords, and let AI handle the rest, with multiple writing styles.
  • Intelligent Extensions: Expand your thoughts with context-aware suggestions.
  • Summarization: Get concise overviews of long emails instantly.
  • Global Reach: Translate your emails into any language with ease.

This feature supports:

  • Smart email replies
  • Optimized content
  • Keyword-based drafts
  • Intelligent content extension
  • Email summarization
  • Multi-language translation

Best of all, this feature is completely free forever! Don’t wait—download AI Mail Assistant now and enjoy!


Best Office Productivity Tools

Breaking News: Kutools for Outlook Launches Free Version!

Experience the all-new Kutools for Outlook with 100+ incredible features! Click to download now!

🤖 Kutools AI : Uses advanced AI technology to handle emails effortlessly, including replying, summarizing, optimizing, extending, translating, and composing emails.

📧 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 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 ...

Use Kutools in your preferred language – supports English, Spanish, German, French, Chinese, and 40+ others!

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

kutools for outlook features1 kutools for outlook features2

🚀 One-Click Download — Get All Office Add-ins

Strongly Recommended: Kutools for Office (5-in-1)

One click to download five installers at once — Kutools for Excel, Outlook, Word, PowerPoint and Office Tab Pro. Click to download now!

  • One-click convenience: Download all five setup packages in a single action.
  • 🚀 Ready for any Office task: Install the add-ins you need, when you need them.
  • 🧰 Included: Kutools for Excel / Kutools for Outlook / Kutools for Word / Office Tab Pro / Kutools for PowerPoint