Link to home
Start Free TrialLog in
Avatar of SysExpert
SysExpertFlag for Israel

asked on

Need script to give name and count of email in all outlook folders to a text file

I am trying to find some missing emails by comparing PST backups. Apparently some emails may have been moved accidentally, and I need a count of All emails per folder, with name of folder, and count output to a text file.
This should include not just inbox folders, but everything.

I am normally a Lotus NOTES and Excel scripter, so I can't quite figure this one out.

similar scripts  that do part of it
https://www.experts-exchange.com/questions/22959025/Need-to-get-the-Count-according-to-subject-from-outlook-mails.html


http://blogs.technet.com/kclemson/archive/2007/08/01/count-the-number-of-folders-in-your-mailbox-with-outlook-vba.aspx

Thanks !
Avatar of David Lee
David Lee
Flag of United States of America image

Hi, SysExpert.

This should do it.  Follow these instructions to use this.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects and click on Module1
4.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
5.  Edit the code as needed.  I included comments wherever something needs to or can change
6.  Click the diskette icon on the toolbar to save the changes
7.  Close the VB Editor
8.  Run the macro CountMessages
Dim objFile As Object
 
Sub CountMessages()
    Dim objFSO As Object, objStore As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Change the file name and path on the next line'
    Set objFile = objFSO.CreateTextFile("C:\eeTesting\Folder Count.txt")
    For Each objStore In Session.Stores
        objFile.WriteLine "*** Store = " & objStore.DisplayName
        CountFolder objStore.GetRootFolder
        objFile.WriteLine
    Next
    objFile.Close
    Set objFile = Nothing
    Set objFSO = Nothing
    MsgBox "Finished"
End Sub
 
Sub CountFolder(olkFolder As Object)
    Dim olkSubFolder As Object
    objFile.WriteLine olkFolder.Name & " = " & olkFolder.Items.Count
    For Each olkSubFolder In olkFolder.Folders
        CountFolder olkSubFolder
    Next
    Set olkSubFolder = Nothing
End Sub

Open in new window

Avatar of SysExpert

ASKER

Thanks for the help !

I get an error on line 8

      
get Run-time error '438';
Object doesn't support this property or method

Using outllook 2003 SP3 on Exchange 2003 , fully patched

Running from outlook client.

Can you please test again ?

Thanks !




That code is for Outlook 2007 not 2003.  Apologies, since no version was specified I wrote for the latest version.  I'll see about making changes to get it to work from 2003 and post again.
found this

http://www.microsoft.com/technet/scriptcenter/resources/qanda/jun06/hey0616.mspx

but it does not work either.

The 2 I have listed in my first post, sem to work, but do not give me the info I want ( only folder count, not items in the folder )

Thanks !
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great !

WOrks perfectly !

Cool!