I am trying to get a cell in excel to display a total count of emails in a given subfolder from a specific outlook account (which is a shared mailbox).
Two questions: How to specify which Outlook email to look in, and how to resolve the compile error, "Next without For"
I have this so far:
Sub CountPBI()
Dim olApp As Object
Dim olNS As Object
Dim olFolder As Object
Dim olMail As Object
Dim itemCount As Long
Dim i As Long
' Create an Outlook application object
Set olApp = CreateObject("Outlook.Application")
' Get the Outlook namespace
Set olNS = olApp.GetNamespace("MAPI")
' Specify the folder (e.g., Inbox)
Set Inbox = NS.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("Projects").Folders("Natural Gas").Folders("Williams/Transco").Folders("NESE").Folders("Public Comments").Folders("Individuals")
' Check if it's a mail item (and potentially other criteria)
If TypeName(olMail) = "MailItem" Then
' Add your counting logic here, e.g., check subject, date, etc.
itemCount = itemCount + 1
End If
Next i
' Display the count in a cell (e.g., cell A1)
ThisWorkbook.Sheets("Sheet1").Range("B4").Value = itemCount
' Clean up objects
Set olMail = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub
Next ibut no openingForand you do not setolMailanywhere in the code. Seems like a chunk is missing. And for accessing a shared mailbox: stackoverflow.com/questions/71413953/…