
On her Outlook Tips Blog, Diane Poremsky has a poll asking how many email accounts her readers have. I realize the poll is old, but here's some short VBA code that can answer the question for you.
Count the Number of Mailboxes in Outlook
This function, placed in a standard module in Outlook's VB Editor, returns the number of mailboxes present.
Function MailBoxesCount() As Long
Dim ol As Outlook.Application
Dim olNS As Outlook.NameSpace
Set ol = Outlook.Application
Set olNS = ol.GetNamespace("MAPI")
MailBoxesCount = olNS.Folders.Count
End Function
If you need the names of each mailbox, iterate through the Folders collection. For example,
Dim ol As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim i As Long
Set ol = Outlook.Application
Set olNS = ol.GetNamespace("MAPI")
For i = 1 To olNS.Folders.Count
MsgBox olNS.Folders(i).Name
Next i
This code can have many uses, for example when you're looping through folders and need to check every mailbox. Return the name of the topmost folder and then walk down the hierarchy.
Happy Halloween!
Follow Me