'How to read mails stored on Exchange server when Outlook only downloads the latest messages?

I'm connected to an Exchange server, so some mails are stored in my local Outlook file and some on the server.

In Outlook the item count of the folder shows 13.000 items.

When I try to iterate over the items via VBA:

Sub asdf()
Dim objOutlook As Outlook.Application
Set objOutlook = CreateObject("Outlook.Application")
Dim objMAPIFolder As Object
Dim objFolder As Object
Dim myItem As Outlook.MailItem
Dim archivFolder As Outlook.Folder
Dim subFolder As Outlook.Folder
For Each objMAPIFolder In objOutlook.Session.Folders
    For Each objFolder In objMAPIFolder.Folders
        If objFolder.Name = "Inbox" Then
            Set archivFolder = objFolder
            For Each subFolder In archivFolder.Folders
                If subFolder.Name = "Folder X" Then
                    MsgBox subFolder.Items.Count 'should be 13.000 but says 5.000
                End If
            Next
        End If
    Next
Next
Set objOutlook = Nothing
End Sub

This shows me a count of 5.000.

In Outlook I can click on "Click here to view more on Microsoft Exchange" and see the rest of the mails.

How can I iterate over the server based mails via VBA?



Solution 1:[1]

On the Extended MAPI level (C++ or Delphi), you can open the folder in the online mode to access the remotely stored messages.

In case of VBA (or other languages, such as C# or VB.Net) and if using Redemption (I am its author) is an option, it can open the folder in the online mode (its RDOSession.GetFolderFromID method allows to pass the MAPI_NO_CACHE flag):

     MAPI_NO_CACHE = &H200
     MAPI_BEST_ACCESS = &H10
     set Session = CreateObject("Redemption.RDOSession")
     Session.MAPIOBJECT = objOutlook.Session.MAPIOBJECT
...
     set rFolder = Session.GetFolderFromID(subFolder.EntryID, , MAPI_NO_CACHE + MAPI_BEST_ACCESS)
     MsgBox rFolder.Items.Count

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1