For Each ... in MAPIFolder.Items leads to an error?!

  • Thread starter Thread starter Manuel Graumann
  • Start date Start date
M

Manuel Graumann

Hi folks!

I'm automating the deletion of some Junk-Mails using this code:

Private Sub Delete_Junk()
Dim objPosteingang As MAPIFolder
Dim objNewMail As MailItem

Set objPosteingang =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

For Each objNewMail In objPosteingang.Items
'(...)
Next

Set objPosteingang = Nothing
End Sub

If I receive a MeetingItem, which is usually an item of the specified
folder, the code stops with an error.
I already found out, that it happens at "for each..." but WHY?

I thought this can't happen as objNewMail is defined as MailItem and not
simply as Item?!

Yes, I could write some workaround for myself but that's not the point I'm
after.

If anybody could give me some more insight into this matter, I'd appreciate
it.

Regards

Manuel
 
Declaring objNewMail as MailItem doesn't mean that the code will ignore any
items that aren't messages. You should declare it as Object.
 
Hello Sue!

Thank you for your reply.

If I declare x as integer and try to assign the value of x to a function
which expects the value to be long VB does the necessary transformation.
Why has nobody implemented this 'feature' to "For Each x in Collection" ?
Would have been more logical to me.

Regards

Manuel
 
Not to mention writing an If statement inside the loop to
make sure the current item IS a mailItem before doing
anything with it! This one caught me recently, as Sue may
remember, since she helped me with it... :-)

-Andrew
 
Objects are different.

Manuel Graumann said:
Hello Sue!

Thank you for your reply.

If I declare x as integer and try to assign the value of x to a function
which expects the value to be long VB does the necessary transformation.
Why has nobody implemented this 'feature' to "For Each x in Collection" ?
Would have been more logical to me.

Regards

Manuel
 
Back
Top