+ Reply to Thread
Results 1 to 9 of 9

Check if a mailbox folder exists

Hybrid View

  1. #1
    Registered User
    Join Date
    02-17-2009
    Location
    Sydney
    MS-Off Ver
    Excel 2003
    Posts
    21

    Check if a mailbox folder exists

    Hey Guys

    I have the following code which counts items in a mailbox:

    Sub CountMail()
        
    Dim ns As Namespace
    Dim mailbox As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer
    
    Set ns = GetNamespace("MAPI")
    
    Set mailbox = ns.Folders("Mailbox - BOB").Folders("Inbox")
    EmailItemCount = mailbox.Items.Count
    Worksheets("Sheet1").Cells(3, 2) = EmailItemCount
    
    Set mailbox = ns.Folders("Mailbox - JOHN").Folders("Inbox")
    EmailItemCount = mailbox.Items.Count
    Worksheets("Sheet1").Cells(4, 2) = EmailItemCount
    
    End Sub
    I want to be able to run a check to see if the mailbox exists before I do an email count.

    So I need an IF statement to check if mailbox exists in destination, if yes then run email count and put in Cells(x,y) else if mailbox doesnt exist then put "N\A" in Cells(x,y).

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Check if a mailbox folder exists

    Why so complicated ?
    Sub CountMail()
      on error resume next
      With GetNamespace("MAPI")
         x=.Folders("Mailbox - BOB").Folders("Inbox").Items.Count
         y=.Folders("Mailbox - JOHN").Folders("Inbox").Items.Count
      if err.number >0 then msgbox "at least 1 inbox doesn't exist"
    End Sub
    Thsi code runs in Outllook. (not in Excel)

  3. #3
    Registered User
    Join Date
    02-17-2009
    Location
    Sydney
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Check if a mailbox folder exists

    I need this to run in Excel...

    Its a scheduled report that I run everyday on about 10 mailboxes. Its counts all the items in each mailbox.

    If someone else runs it, i want it to show which mailboxes they dont have.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Check if a mailbox folder exists

    From Excel

    Sub CountMail()
      on error resume next
      sq=split("BOB|JOHN|AngelA|Mary","|")
      With createobject("Outlook.Application").GetNamespace("MAPI")
         for j=0 to ubound(sq)
           x=.Folders("Mailbox - " & sq(j)).Folders("Inbox").Items.Count
           if err.number = 0 then sq(j)=""
           if err.number>0 then err.clear 
         next
      End with
      msgbox "unavailable mailboxes " & join(sq)
    End Sub

  5. #5
    Registered User
    Join Date
    02-17-2009
    Location
    Sydney
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Check if a mailbox folder exists

    Ummm This is not what I want. Thanks for your help though.

    For Each mailbox I need a count of all the items in the Inbox. These are then displayed in a particular cell that I assign it to. I have already done this. See the Code above.

    The only thing I am unable to do is:

    If the mailboxes is not available, in that desginated Cell in Excel I want it to display N\A, instead i am getting error messages.
    I know how to clear them, but instead I want it to display N\a in the cell.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Check if a mailbox folder exists

    Try this:
    Sub CountMail()
        
    Dim ns As Namespace
    Dim mailbox As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer
    
    Set ns = GetNamespace("MAPI")
    On Error Resume Next
    Set mailbox = ns.Folders("Mailbox - BOB").Folders("Inbox")
    If mailbox Is Nothing Then
        Worksheets("Sheet1").Cells(3, 2) = "N/A"
    Else
        Worksheets("Sheet1").Cells(3, 2) = mailbox.Items.Count
    End If
    Set mailbox = Nothing
    
    Set mailbox = ns.Folders("Mailbox - JOHN").Folders("Inbox")
    If mailbox Is Nothing Then
        Worksheets("Sheet1").Cells(4, 2) = "N/A"
    Else
        Worksheets("Sheet1").Cells(4, 2) = mailbox.Items.Count
    End If
    
    End Sub
    Everyone who confuses correlation and causation ends up dead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1