# Wednesday, April 05, 2006
« The MiniDisc Mixes (Mix 9) | Main | Explain the minidisc mix order »

At Microsoft we get a fair bit of mail with all the distribution lists. The 'Mark All as Read' command only operates on the current folder so I knocked together a macro to do the recursion. The code below iterates the selected folder and it's children marking all the mails as 'read'. There may be optimisations as my VBA is a bit rusty so I'd love to hear what they are.

Sub RecursiveMarkAsRead()
  MarkFolderAsRead Application.Explorers.Item(1).CurrentFolder, True
End Sub

Sub MarkFolderAsRead(ByVal parent As MAPIFolder, ByVal recurse As Boolean)
  On Error GoTo ErrorHandler
  If parent.UnReadItemCount > 0 Then
    Dim item As Object
    For Each item In parent.Items
      If TypeName(item) = "MailItem" Then
        Dim mail As MailItem
        Set mail = item
        mail.UnRead = False
      End If
    Next
  End If

  If recurse = True Then
    Dim folder As MAPIFolder
    For Each folder In parent.Folders
      MarkFolderAsRead folder, recurse
    Next
  End If

Exit Sub
ErrorHandler:
' Ignore this folder
End Sub

by This posting is provided "AS IS" with no warranties, and confers no rights.
posted on Wednesday, April 05, 2006 4:12:58 PM (GMT Daylight Time, UTC+01:00)  #    Comments [1] Trackback
Related posts:
Debugging FullTrust VSTO InfoPath Forms
Link Dump
Customer Day Presentation
Send a smile...
Office and Vista Beta 2 Release
Sunday, April 09, 2006 5:26:30 PM (GMT Daylight Time, UTC+01:00)
Ah, VBA - the development tool of champions! :-)

Well, technically, VBScript is the development tool of champions - but VBA is close enough...
Comments are closed.