Type Mismatch error in a collection of items

  • Thread starter Thread starter Susan Lammi
  • Start date Start date
S

Susan Lammi

This code is throwing a Type Mismatch error on the line "Set
mmsg=fldInItems(icnt)" but only if the previous message is moved. If none
of the messages are moved the code loops thru the entire collection
without issue.

What am I missing here.....??????

Note: fldInItems is a collection of Unread mail messages in the "InBox"

dim fldInItems as Items
dim mmsg as MailItem

Do Until icnt > fldInItems.Count
Set mmsg = fldInItems(icnt)
If InStr(1, mmsg.Subject, "!#Metrics#!") <> 0 And Not
IsNull(mmsg.Subject) Then
mmsg.Move fldMetrics
Else
icnt = icnt + 1
End If
Loop
 
If I understand what you are suggesting, Yes I thought of that and will
add the code to check that the item is in fact a mail message but in
testing I am sure the item is in fact a mail message. Why is it
failing....????

Sue
 
Never assume a homogeneous collection of items when you loop through an
Items collection. Declare your basic item as Object and check its Class
property before invoking item-specific properties or methods. An On Error
Resume Next statement can also do wonders in these cases.
 
I don't know. You didn't show how iCnt is being set. I prefer to use a
countdown loop myself:

intCount = fldInItems.Count
For i = intCount to 1 Step -1
Set mmsg = fldInItems(i)
' do stuff
mmsg.Move fldMetrics
Next


--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
So much more elegant, and better yet it works......
although I'm not sure why the other did not work
I suspect the collection was confused when ever I moved an item out
(like that for a technical explaination???)

In any case Thanks again..... I am forever in your debt.......

Sue
 
Susan-

You're right. Whenever you are deleting items in a loop,
always use a "count-down" loop. Otherwise things get
confused because every time you remove an item, the
position of the items in the collection changes- the item
that was at position #2 is now at position #1, etc. When
you remove items from the "top end" first, no "re-
numbering", or position change, occurs & everything is OK.

Actually, it's not VBA, or the Collection, getting
confused; if you think about it, VBA is "doing the right
thing"; you're asking VBA to "do what I MEANT, not what I
said!" When you delete item #1, everything "slides down"
one, and when you delete "the next item", at #2, you're
actually deleting the item that WAS at position #3- so
item #2 gets skipped and is not processed - and/or, you
get the kind of hard-to-diagnose error you received, when
you get to the end of the Collection.

Hope that clears things up a little.

Also, take Sue's advice & always check the type of the
item, even if you're sure there is only one type of item
in the Collection. Contacts folders can contain
Distribution Lists as well as mail items, Inboxes can
contain PostItems, etc. So check the type with an IF
statement before you process each item. This one tripped
me up once, and it took quite awhile before I figured out
what was causing the problem- the error message only makes
sense once you understand what the problem is!

HTH,

-Andrew
====================================================
 
Back
Top