0

Outlook 2010 VBA.
Trying to search for flagged messages in all my folders.
Created this:

Private Sub flagrecurse(fold As Variant)
    Dim olItem As MailItem
    Dim nxtfold As Folder
    Dim olFoldVar As Variant

    'Test for which folder is being checked
    MsgBox (fold.Name)
    
    If fold.Folders.Count > 0 Then
        For Each nxtfold In fold.Folders
            Set olFoldVar = nxtfold
            flagrecurse olFoldVar
        Next nxtfold
    Else
        For Each olItem In fold.Items
             'Test for which item is being checked
             If TypeName(olItem) = "MailItem" Then
                MsgBox (olItem.Subject)
                With olItem
                    If .FlagRequest <> "" Or .IsMarkedAsTask Then
                        '.TaskDueDate = Now
                        'Sets a reminder today, in case one wasn't set
                        If Not (.ReminderSet) Then
                            .ReminderSet = True
                            .ReminderTime = Now + 2 / 24
                            .Save
                        End If
                    End If
                End With
            End If
        Next olItem
    End If
End Sub

The issue is I have 744 items in my Inbox, it gets through 8 or 9, then gives me a "Type Mismatch". When I'm debugging, it flags Next olItem and when I check the value of olItem it resolves to Nothing.

What am I doing wrong?

GSerg
  • 76,472
  • 17
  • 159
  • 346
Trashman
  • 1,424
  • 18
  • 27

1 Answers1

3

You are assuming that you can only have MailItem objects in the folder. You can also have ReportItem, MettingRequestItem, etc. Declare olItem as a generic Object and check the Class property to be 43 (olMail).

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I knew there were other objects in the folder. My assumption was that for each would only look at items that matched the type of the variable I was using (rather than making the variable null). Either way, your suggestion worked. Thank You! – Trashman Jun 23 '14 at 20:46
  • Bloody legend. This was making me sad! – SSlinky Jul 22 '16 at 10:45