First part of an introduction to stores and the folders they contain. Contains macros to display (1) the names of accessible stores and (2) the names of accessible stores and the top level folders within them.
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.
StoresColl in macros
ListStores2() are both collections but hold different types of object:
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”.
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.
In my Folder Pane example above, I only list three standard folders: “Inbox”, “Drafts” and “Deleted Items”. There are other standard folders and you can create as many folders of your own as you wish. Some people create folders under Inbox but I prefer to create new folders at the same level as Inbox. Your folders can have sub-folders which can have their own sub-folders to any depth.
The following macro will produce a listing of the form:
A A1 A2 A3 B B1 B2 C C1 C2 C3 C4
where A, B and C are stores and A1, B1, C1 and so on are folders within A, B and C. If A1, B1, C1 and so on have sub-folders, they will not be listed by this macro. Accessing more deeply nested folders will be covered in the next part of this tutorial.
Sub ListStoresAndTopLevelFolders() Dim FldrCrnt As Folder Dim InxFldrCrnt As Long Dim InxStoreCrnt As Long Dim StoreCrnt As Folder With Application.Session For InxStoreCrnt = 1 To .Folders.Count Set StoreCrnt = .Folders(InxStoreCrnt) With StoreCrnt Debug.Print .Name For InxFldrCrnt = .Folders.Count To 1 Step -1 Set FldrCrnt = .Folders(InxFldrCrnt) With FldrCrnt Debug.Print " " & .Name End With Next End With Next End With End Sub
Confession: I do not remember either of the “Hows”. I have subroutines and functions that remember for me.