Problem looping thru Mailitems

  • Thread starter Thread starter John Douglas
  • Start date Start date
J

John Douglas

I am trying to loop thru all messages in my inbox with the following code:

Dim tempMailItem As Outlook.MailItem
Dim iBox As Outlook.MAPIFolder
Dim LastTim As Date

Set iBox = Application.Session.GetDefaultFolder(olFolderInbox)

iBox.Items.Sort "[Received]"

For Each tempMailItem In iBox.Items
Debug.Print tempMailItem.Subject
Next


It makes it thru a fair amount of the items but it always finds a type
mismatch. Any clue why or what I can do to? Is there a variable I could
use to try to find all items (Not just mail items) in the inbox? Thank you.
 
It appears that this will happen if you hit an item that is not a mail item,
such as a meeting request or cancellation or something along those lines.
I'm not Outlook programmer, but I imagine if you check for the item's type
and verify that it's an olMailItem, you should be okay.

Ray at work
 
That's exactly it. The property to check is Class.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Ray at said:
It appears that this will happen if you hit an item that is not a mail item,
such as a meeting request or cancellation or something along those lines.
I'm not Outlook programmer, but I imagine if you check for the item's type
and verify that it's an olMailItem, you should be okay.

Ray at work


John Douglas said:
I am trying to loop thru all messages in my inbox with the following code:

Dim tempMailItem As Outlook.MailItem
Dim iBox As Outlook.MAPIFolder
Dim LastTim As Date

Set iBox = Application.Session.GetDefaultFolder(olFolderInbox)

iBox.Items.Sort "[Received]"

For Each tempMailItem In iBox.Items
Debug.Print tempMailItem.Subject
Next


It makes it thru a fair amount of the items but it always finds a type
mismatch. Any clue why or what I can do to? Is there a variable I could
use to try to find all items (Not just mail items) in the inbox? Thank you.
 
Rather than checking class, don't explicitly declare (late binding) your
variables ie

Dim oOutlook As Object
Dim oMail As Object
Dim oNameSpace As Object
Dim oMailBox As Object
Dim oMBItems As Object

Set oOutlook = CreateObject("Outlook.application")

Set oNameSpace = oOutlook.GetNamespace("MAPI")
Set oMailBox = oNameSpace.GetDefaultFolder(6)
Set oMBItems = oMailBox.Items

For Each oMail In oMBItems
If oMail.Subject = sEmailSubject Then
' ........
Exit For
End If
Next oMail
 
I like jp savory's approach, but would question some variable names
(just for clarity's sake). For instance

Dim oMail As Object

would be best replaced by

Dim item as Object

Then again you do not even need to declare it:

Dim mapiNameSpace As NameSpace
Dim inbox As MAPIFolder
Dim inboxItems As Items

Set mapiNameSpace = Application.GetNamespace("MAPI")
Set inbox = mapiNameSpace.GetDefaultFolder(olFolderInbox)
Set inboxItems = inbox.Items

For Each item In inboxItems
'deal with item
Next

When 'dealing with item' you must make sure that all possible items in
the inbox understand any methods you use as a pre-condition.
 
Back
Top