-1

My folder is called "Request Mailbox" in Outlook

How can I get a list of all mailitems in that folder

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
software is fun
  • 7,286
  • 18
  • 71
  • 129

2 Answers2

2

To get list of MailItems, you can simply do this

MailItem list will be displayed as Email

Option Explicit
Sub MailItems()
    Dim olNamespace As Outlook.NameSpace
    Dim olFolder  As Outlook.MAPIFolder
    Dim olItem As Outlook.MailItem

    Set olNamespace = Application.GetNamespace("MAPI")
    Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox).Folders("Request Mailbox")

    Set olItem = Application.CreateItem(olMailItem) ' Creat EMail
    With olItem
        For Each olItem In olFolder.Items
            Debug.Print olItem.Subject ' Print to immediate window
            .body = .body & olItem.Subject & vbCrLf ' Print to Email
            Debug.Print olItem.SenderName
            .body = .body & olItem.SenderName & vbCrLf
            Debug.Print olItem.ReceivedTime
            .body = .body & olItem.ReceivedTime & vbCrLf & vbCrLf
        Next ' vbCrLf = vb: Visual Basic Cr: Carriage Return Lf: LineFeed
        .Subject = "Mail Items" ' Subject
        .Display    ' Display Msg
    End With

End Sub

For Shared Folder Try this

Option Explicit
Sub ShareMailItems()
    Dim olNamespace As Outlook.NameSpace
    Dim olShareName As Outlook.Recipient
    Dim olShareInbox As Outlook.Folder
    Dim olItem As Outlook.MailItem

    Set olNamespace = Application.GetNamespace("MAPI")
    Set olShareName = olNamespace.CreateRecipient("0m3r@email.com") '// Owner's email address
    Set olShareInbox = olNamespace.GetSharedDefaultFolder( _
                 olShareName, olFolderInbox).Folders("Request Mailbox") '// FolderName

    Set olItem = Application.CreateItem(olMailItem) ' Creat EMail
    With olItem
        For Each olItem In olShareInbox.Items
            Debug.Print olItem.Subject ' Print to immediate window
            .body = .body & olItem.Subject & vbCrLf ' Print to Email
            Debug.Print olItem.SenderName
            .body = .body & olItem.SenderName & vbCrLf
            Debug.Print olItem.ReceivedTime
            .body = .body & olItem.ReceivedTime & vbCrLf & vbCrLf
        Next ' vbCrLf = vb: Visual Basic Cr: Carriage Return Lf: LineFeed
        .Subject = "Mail Items" ' Subject
        .Display    ' Display
    End With
End Sub
0m3r
  • 12,286
  • 15
  • 35
  • 71
1

For a delegate mailbox already open in Outlook, use Application.Session.Folders.("TheDelegateMialboxName@YourCompany.com").Folders("TheFolderName")

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I guess I miss that part, If I am not mistaken there is no `"."` between `Folders.("The` ? – 0m3r Jun 24 '15 at 05:43
  • Yes, you are right. Or you can expliiclty specify the Item function instead of relying on default property: `Application.Session.Folders.Item("TheDelegateMialboxName@YourCompany.com").Folders.Item("TheFolderName")` – Dmitry Streblechenko Jun 24 '15 at 06:09