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

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
 
S

Sue Mosher [MVP]

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

Manuel Graumann

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
 
A

Andrew Cushen

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
 
S

Sue Mosher [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top