3

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jacob Quisenberry
  • 1,131
  • 3
  • 20
  • 48
  • Is there something specific you are doing with each store? You can loop through each mailbox in Outlook 2003 but depending on what you are doing, it may or may not be possible. – JimmyPena Nov 07 '11 at 20:05
  • First I want to populate a list box with the store display name, store file path, and store ID. The user will then select a store from the list and click a button. That will save the StoreID in a string variable. I will then use the NameSpace.GetItemFromID method to get a MailItem. Finally, I will call MailItem.Display. – Jacob Quisenberry Nov 07 '11 at 22:17

1 Answers1

4

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
JimmyPena
  • 8,694
  • 6
  • 43
  • 64
  • Thank you. By store file path, I mean the path and filename of the PST file, as in C:\test.pst – Jacob Quisenberry Nov 08 '11 at 17:37
  • In that case, this link might help: http://stackoverflow.com/questions/195849/how-to-find-full-path-of-outlook-pst-file – JimmyPena Nov 08 '11 at 18:28
  • I will not be able to use the Redemption DLL. I will not be able to control what software is on the executing machine, except that it will run Outlook 2003. I need to use VBA exclusively. – Jacob Quisenberry Nov 10 '11 at 01:47
  • That page mentions Redemption, but the code sample doesn't use it. – JimmyPena Nov 10 '11 at 01:51
  • 1
    That is the last piece of the puzzle! That code taught me that the store ID includes the ANSI code points of the absolute path to the mail store. I would never have guessed that. This also explains why the StoreID may differ from machine to machine and why the length of the StoreID is not a constant. Outstanding. – Jacob Quisenberry Nov 12 '11 at 00:18