1

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

3
  • You have Next i but no opening For and you do not set olMail anywhere in the code. Seems like a chunk is missing. And for accessing a shared mailbox: stackoverflow.com/questions/71413953/… Commented Jul 8 at 17:36
  • You could adapt the code on this link. davetallett26.github.io/excel-moveitems.html Commented Jul 8 at 19:18
  • Thanks! Unfortunately, I am unsure how to rewrite this to adapt it. By new to VBA, I mean very new with little to no coding experience outside of very basic automatic in Python. Commented Jul 9 at 17:28

1 Answer 1

0

Not sure if it's going to work with shared mailboxes as I have no way to test it but it allows for easy folder selection:

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")

    ' Prompt user to select a folder
    Set olFolder = olApp.Session.PickFolder

    ' Exit if no folder is selected
    If olFolder Is Nothing Then
        MsgBox "No folder selected.", vbExclamation
        Exit Sub
    End If

    ' Count mail items
    For i = 1 To olFolder.Items.Count
        Set olMail = olFolder.Items(i)
        If TypeName(olMail) = "MailItem" Then
            itemCount = itemCount + 1
        End If
    Next i

    ' Display the count in a cell
    ThisWorkbook.Sheets("Sheet1").Range("B4").Value = itemCount

    ' Clean up
    Set olMail = Nothing
    Set olFolder = Nothing
    Set olNS = Nothing
    Set olApp = Nothing

End Sub


Sign up to request clarification or add additional context in comments.

2 Comments

Michael, that works! I can select the right folder to pull it in. I am just not sure how to run the Macro from the spreadsheet itself, i.e., I have to run the module from Macro window itself. =CountPBI() in a cell returns an error.
"=CountPBI() in a cell returns an error" - if you want to call a macro from a worksheet cell the called method needs to be a Function, not a Sub, and it should return the value to the calling cell. A Function called from a worksheet formula can't directly update the worksheet - that is one of the limitations of calling a Function that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.