vbaoutlookpstoutlook-2003

Loop Through PSTs in Outlook 2003 with VBA


In Outlook 2007, I am able to loop through mails stores, including PSTs, using code like this:

Dim stores As Outlook.stores
Set stores = objNamespace.stores
Dim store As Outlook.store

For Each store In stores
    MsgBox store.FilePath
Next

However, in Outlook 2003, the Outlook.store and Outlook.stores objects do not exist.

Are there equivalent objects in Outlook 2003? What other method might I use to loop through mail stores?

Thank you.


Solution

  • This sample code for Outlook 2003 will loop through the high level mailboxes and print certain properties to the Immediate Window. I chose the properties that looked most useful based on your request.

    Sub LoopThruMailboxes()
    
    Dim olApp As Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim mailboxCount As Long
    Dim i As Long
    Dim folder As Outlook.MAPIFolder
    
    ' get local namespace
    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    
    mailboxCount = olNS.Folders.count
    
    For i = 1 To mailboxCount
      Set folder = olNS.Folders(i)
    
      Debug.Print folder.EntryID
      Debug.Print folder.StoreID
      Debug.Print folder.Name
      Debug.Print folder.FolderPath
    Next i
    
    End Sub
    

    folder.Name is the name of the mailbox, folder.StoreID is the store ID (I'm not sure what you meant by "store file path", I didn't see anything that looked relevant anyway).

    Here's a functionized version that returns folder name and store ID as an array, which you could assign directly to a listbox:

    Function GetMailBoxInfo() As String()
    
    Dim olApp As Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim mailboxCount As Long
    Dim i As Long
    Dim folder As Outlook.MAPIFolder
    Dim tempString() As String
    
    ' get local namespace
    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    
    mailboxCount = olNS.Folders.count
    
    ' size array accordingly
    ReDim tempString(1 To mailboxCount, 1 To 2)
    
    For i = 1 To mailboxCount
      Set folder = olNS.Folders(i)
    
      tempString(i, 1) = folder.Name
      tempString(i, 2) = folder.StoreID
    Next i
    
      GetMailBoxInfo = tempString
    
    End Function
    

    ex:

    ListBox1.List = GetMailBoxInfo