That error means that the folder does not exist in that "area" of Outlook.
If you debug your code and observe the contents of
you will realise that it contains the list of mailboxes, not the list of folders within a mailbox.
You need to "point" to the appropriate mailbox first, then get the list of folders e.g.
Dim objItems As Variant
Set objItems = Session.GetDefaultFolder(olFolderInbox).Parent.Folders
There are methods for finding other mailboxes (shared, invite etc) described here - Working with VBA and non-default Outlook Folders
You will still need to handle this error appropriately as you are relying entirely on someone entering the name of the folder correctly. There are other issues with your code too …
* You should fully qualify the range that you are using e.g.
ThisWorkbook.Sheets(1).Range("A" & ThisRow).Value
* You should avoid using
... why use two lines of code when one will do but importantly other code or user actions could "grab" the ActiveCell.
Range("B" & ThisRow).Select
ActiveCell.Value = EmailCount
ThisWorkbook.Sheets(1).Range("B" & ThisRow).Value = EmailCount
* You increment
before you update the count onto the worksheet so everything will be offset by 1
Personally I would have used a For Each loop rather than relying on a list being kept up to date and you will probably have to have a recursive call to handle sub-folders.
Have a look at vba - Can I iterate through all Outlook emails in a folder including sub-folders? - Stack Overflow
Probably something like the following - although note I have not tested this fully
Dim objOL As Object
Set objOL = CreateObject("Outlook.Application")
Dim objNS As Object
Set objNS = objOL.GetNamespace("MAPI")
Dim EmailCount As Integer
Dim ThisRow As Integer
ThisRow = 0
Dim count As Long
Dim objFolder As Variant
For Each objFolder In objNS.Folders
count = 0
ThisRow = ThisRow + 1
ThisWorkbook.Sheets(1).Range("A" & ThisRow).Value = objFolder.Name
ThisWorkbook.Sheets(1).Range("B" & ThisRow).Value = objFolder.Items.count
count = count + HowManyEmailsInFolder(objFolder)
Private Function HowManyEmailsInFolder(ByVal oParent As Outlook.MAPIFolder) As Long
Dim oFolder As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem
Dim numEmails As Long
numEmails = numEmails + oParent.Items.count
If (oParent.Folders.count > 0) Then
For Each oFolder In oParent.Folders
numEmails = numEmails + HowManyEmailsInFolder(oFolder)
HowManyEmailsInFolder = numEmails