StartDownloadsServiceSamplesWorkshopsContact DeutschEnglish
Awarded by
Microsoft since 2005:
mvp logo
VBOffice Info
Privacy Policy
Limited loops
Author: Michael BauerHomepage
Date: 02.12.2006Accessed: 24090

Sometimes it isn't possible to loop through more than about 250 items because Outlook doesn't dispose the object references - although you set the variable explicitly to Nothing.

A workaround is to move the code for the loop into a separate procedure and leave that after a limited number of loops, in this sample after 250 loops. If the object variable goes out of scope all references are definitely disposed. Then the procedure can be called again.

Option Explicit

Public Sub LoopFolder(Folder As Outlook.MAPIFolder)
  Dim Cnt As Long
  Dim Limit As Long
  Dim CountFrom As Long
  Dim CountTo As Long
  Dim Done As Long
  Dim Items As Outlook.Items

  Set Items = Folder.Items
  Cnt = Items.Count

  ' Grenze pro Durchlauf festlegen.
  Limit = 250

  If Cnt <= Limit Then
    LoopItems Items, 1, Cnt
    Do While Done < Cnt
      CountFrom = Done + 1
      CountTo = Done + Limit
      LoopItems Items, CountFrom, CountTo
      Done = CountTo
      If Done = Cnt Then
        Exit Do
      ElseIf Cnt - Done <= Limit Then
        Limit = Cnt - Done
      End If
  End If
End Sub

Private Sub LoopItems(Items As Outlook.Items, _
  ByVal CountFrom As Long, _
  ByVal CountTo As Long _
  Dim i As Long
  Dim obj As Object
  Dim Mail As Outlook.MailItem

  For i = CountFrom To CountTo
    Set obj = Items.Item(i)
    If TypeOf obj Is Outlook.MailItem Then
      Set Mail = obj
      ' Irgendwas mit dem Objekt machen
    End If
End Sub


ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the ... [more]


Access the master category list in the blink of an eye, share your categories in a network, get a reminder service, and ... [more]


SAM automatically sets the sender, signature, and folder for sent items, for instance based on the recipient ... [more]


OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or ... [more]