MailItem methods failing

T

Tim Baur

Hello all,

I have an odd problem, and haven't been able to determine a solution by
searching the archives. I'm hoping someone here can quickly spot what
I'm doing wrong.

I'm writing a function that searches an Exchange Inbox and returns a
collection of mail items for processing. The problem occurs when I try
to view the sender name, message body, or almost anything other than the
subject for comparison.

So far I've tried declaring my MailItem variable (oMI) as MailItem,
Variant, and Object. I've tried using

For Each oMI in ...Folders(Inbox).Items

And

For i = 1 to Folders(Inbox).Items.Count
Set oMI = Folders(Inbox).Items(i)

The oMI works fine for the first couple hundred, then simply fails when
resolving the sendername or body.

I've included a snip of my code that pertains to the search.

* * * * * * * *
Public Function GetMailItems(sSubjectMatch As String) As Collection
' Search the given MailBoxName and return all mail item objects _
where Subject LIKE sSubjectMatch
Dim colRetVal As Collection
Dim oMI As MailItem
Dim oItems As Outlook.Items
Dim lItemIdx As Long

Set colRetVal = New Collection

Set oItems = moApp.Session.Folders(msMailBoxName). _
Folders("Inbox").Items

For lItemIdx = 1 To oItems.Count
DoEvents
With oItems(lItemIdx)

If .Subject Like sSubjectMatch Then
If .Class = Outlook.OlObjectClass.olMail Then

' ######## FAILS ON NEXT LINE ########

If .SenderName = "TEST Account" Then
colRetVal.Add oItems(lItemIdx)
End If
End If
End If

End With
Next lItemIdx

Set GetMailItems = colRetVal
End Function
* * * * * * * *

Any thoughts?

Many thanks in advance.
 
S

Sue Mosher [MVP-Outlook]

The most likely cause is that the item in question is not a MailItem. Declare oMI as Object, not MailItem, and then check the value of its Class property before you try to use any MailItem-specific properties or methods.

FYI, there is a newsgroup specifically for general Outlook programming issues "down the hall" at microsoft.public.outlook.program_vba or, via web interface, at http://www.microsoft.com/office/community/en-us/default.mspx?dg=microsoft.public.outlook.program_vba

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
T

Tim Baur

The most likely cause is that the item in question is not a MailItem.
Declare oMI as Object, not MailItem, and then check the value of its
Class property before you try to use any MailItem-specific properties
or methods.

FYI, there is a newsgroup specifically for general Outlook programming
issues "down the hall" at microsoft.public.outlook.program_vba or, via
web interface, at
http://www.microsoft.com/office/community/en-us/default.mspx?dg=microso
ft.public.outlook.program_vba

Thank you for your reply, Sue. Moving the MailItem class check to the top
of my logic fixed the problem. I'm not sure I understand why, but It
appears to be working now.

Thank you also for the correction. I chose the group with the most
traffic. Sorry about that.

-Tim
 

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