I need help specifying a nested folder in Outlook using Excel VBA. I will post the code I'm using below.
I'm able to specify the "Inbox" folder but when I try and specify a folder that is within the "Inbox" folder, the code comes back with the "No such folder" message.
Does anyone know why this is happening to me? If so, how can I fix it?
Option Explicit
Sub HowManyEmails()
Dim objOutlook As Object, objnSpace As Object, objFolder As Object
Dim EmailCount As Integer
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")
On Error Resume Next
Set objFolder = objnSpace.Folders("NoctalkSW").Folders("Inbox")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder."
Exit Sub
End If
EmailCount = objFolder.Items.Count
Set objFolder = Nothing
Set objnSpace = Nothing
Set objOutlook = Nothing
[B2].Value = EmailCount
On Error Resume Next
Set objFolder =
objnSpace.Folders("NoctalkSW").Folders("Inbox").Folders("COMPLETED")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder."
Exit Sub
End If
EmailCount = objFolder.Items.Count
Set objFolder = Nothing
Set objnSpace = Nothing
Set objOutlook = Nothing
[B3].Value = EmailCount
End Sub
Did you try to debug the code? Anyway, try to use the following code:
Also you may try to iterate over folders, see How to: Enumerate Folders.
If you wanna access shared Inbox and subfolder then Work with GetSharedDefaultFolder Method
Code Example