outlook-vba Introduction Part 2: Stores and top-level folders 2.2 Stores

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


Outlook stores emails, calendar items, notes, tasks and so on in files known as Stores. If you look at your Folder Pane you will see something like:

  Deleted Items
  :  :

  Deleted Items
  :  :

:  :

"Aaaaaaaaaa", "Bbbbbbbbbb" and "Cccccccccc" are the user or display names of Stores. I have always accepted Outlook defaults for these names which have changed over the years. Once the default was my name now it is my email address. The filename for these stores may be the same but with an extension such as PST or OST or may be something completely different. A VBA macro needs the user name to access a store and is not concerned with the file names or the extension.

You can have as many stores as you wish. I have “Outlook data file” which was created for me when I installed Outlook. When I added accounts for my email addresses, Outlook created new stores named for the email address such as “JohnDoe@hotmail.com” and “DoeJohn@gmail.com”. To reduce the size of my main store I save old emails in stores with names such “Archive 2015”.

If you are a business user, you may have access to shared stores or to the stores of colleagues.

The macros below show three different ways of listing the stores you can access. I suggest you create a new module to hold the code below and to use F4 to access the module’s properties so you can be name it as “ModIntro” or some other name of your choice. If you completed Part 1 of this series, you will already have such a module.

Copy these macros to a module and test that each gives the same output.

Sub ListStores1()

  Dim InxStoreCrnt As Integer
  Dim NS As NameSpace
  Dim StoresColl As Folders

  Set NS = CreateObject("Outlook.Application").GetNamespace("MAPI")
  Set StoresColl = NS.Folders

  For InxStoreCrnt = 1 To StoresColl.Count
    Debug.Print StoresColl(InxStoreCrnt).Name

End Sub
Sub ListStores2()
 Dim StoresColl As Stores
 Dim StoreCrnt As Store
 Set StoresColl = Session.Stores
 For Each StoreCrnt In StoresColl
   Debug.Print StoreCrnt.DisplayName
End Sub
Sub ListStores3()

  Dim InxStoreCrnt As Long

  With Application.Session
    For InxStoreCrnt = 1 To .Folders.Count
      Debug.Print .Folders(InxStoreCrnt).Name
  End With

End Sub

You will find with VBA that there are often several methods of achieving the same effect. Above I have shown three methods of accessing the stores. You do not need to remember them all – pick your own favourite – but you do need to be aware that there are several methods because other people, whose code you may need to study, will have different favourites.

The variables StoresColl in macros ListStores1() and ListStores2() are both collections but hold different types of object: Store and Folder. A Store object can only reference a file on your disc. A Folder can reference a file on disc but can also reference folders within a store such as “Inbox” and “Sent Items”. Stores, Folders, Store and Folder are all part of the Outlook Object Model. This tutorial series introduces you to the model but it is not a formal definition. If you want a formal definition, type “outlook vba object model” into your favourite search engine. Make sure you look at the VBA version of the model.

Got any outlook-vba Question?