HOWTO: Get the Unread Message Count from an Outlook Folder

Last reviewed: September 29, 1997
Article ID: Q171603
The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Outlook 97

SUMMARY

The following code sample demonstrates how to determine the count of all unread messages on a per folder basis for all folders grouped under a specified Microsoft Outlook folder. The sample assumes that the Microsoft Outlook mail client is installed.

MORE INFORMATION

The code below uses a recursive routine to iterate through a mail folder to produce a list of all its sub-folders and a count of corresponding unread messages. The data is then displayed in the Visual Basic Immediate Window. The code takes advantage of the UnReadItemCount property of the Outlook MAPIFolder object.

  1. Open a Standard EXE project in Visual Basic.

  2. Select References and the Project menu. Select "Microsoft Outlook 8.0 Object Library" (msoutl8.olb) and click OK.

  3. Select Project1 Properties on the Project menu and set the Startup Object to "Sub Main".

  4. Add a Module to the project.

  5. Add the sample code below to Module1.

    NOTE: Modify the FOLDER_TO_OPEN constant as appropriate.

  6. Run the program. The unread message count for the folder FOLDER_TO_OPEN is displayed in the Immediate Window.

SAMPLE CODE

Option Explicit

Private Sub Main()
   Dim olMAPI As Outlook.NameSpace
   Dim Folder As Outlook.MAPIFolder
   Const FOLDER_TO_OPEN = "Mailbox - John Doe"   'Modify as appropriate

   Set olMAPI = GetObject("", "Outlook.Application").GetNamespace("MAPI")
   Call PrintFolderNames(olMAPI.Folders(FOLDER_TO_OPEN), "->")
   Set olMAPI = Nothing
End Sub

Sub PrintFolderNames(tempfolder As Outlook.MAPIFolder, a$)
   Dim i As Integer
   If tempfolder.Folders.Count Then
      Debug.Print a$ & " " & tempfolder.Name & "  ";
      Debug.Print tempfolder.UnReadItemCount
      For i = 1 To tempfolder.Folders.Count
        Call PrintFolderNames(tempfolder.Folders(i), a$ & "->")
      Next i
   Else
      Debug.Print a$ & " " & tempfolder.Name & "  ";
      Debug.Print tempfolder.UnReadItemCount
   End If
End Sub
Keywords          : vb5all vb5howto vbwin GnrlVb kbprg
Technology        : kbvba
Version           : WINDOWS:5.0 97
Platform          : NT WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.