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:
Aaaaaaaaaa
Inbox
Drafts
Deleted Items
: :
Bbbbbbbbbb
Inbox
Drafts
Deleted Items
: :
Cccccccccc
: :
"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 “[email protected]” and “[email protected]”. 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
Next
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
Next
End Sub
Sub ListStores3()
Dim InxStoreCrnt As Long
With Application.Session
For InxStoreCrnt = 1 To .Folders.Count
Debug.Print .Folders(InxStoreCrnt).Name
Next
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.