Reputation: 45
I am trying to iterate through all the mail messages in my Sent Items folder.
The code works fine until it hits a non mail item such as a calendar invitation.
Is there a way to skip over the calendar items in the sent items folder?
Sub Find_Sent_Messages_With_Subject()
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderSentMail)
For Each myItem In myFolder.Items
If InStr(1, myItem.Subject, "xxxxxxxxxxxxxx") > 0 Then
'Stop
End If
Next myItem
End Sub
Upvotes: 3
Views: 1195
Reputation: 12499
Replace
For Each myItem In myFolder.Items
With
For i = myFolder.Items.Count To 1 Step -1 '<- backwards
Set myItem = myFolder.Items(i)
Debug.Print myItem
Next i
Or try adding object.class
while looping through
If myItem.Class = olMail Then
Example:
Dim myItem As Object
For Each myItem In myFolder.Items
If myItem.Class = olMail Then
Debug.Print myItem
End If
Next myItem
Edit
Tested outlook 2010
Option Explicit
Sub Find_Sent_Messages_With_Subject()
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim myItem As Object
Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderSentMail)
For Each myItem In myFolder.Items
If myItem.Class = olMail Then
If InStr(1, myItem.Subject, "hello") > 0 Then
Debug.Print myItem
End If
End If
Next myItem
End Sub
Upvotes: 3