Why do I get a type mismatch when trying to access mail items?

  • Thread starter Pete Dawson (Leeds Uni)
  • Start date
P

Pete Dawson (Leeds Uni)

Trying to scan through mail messages in Outlook 2003 SP2. The code below
works OK if the messages are in the Inbox but not if they are in a mail
subfolder. I created this code from an earlier macro that worked fine in
Outlook XP. As you can tell from the variable names, most of it came from
Outlook VBA help. I tried declaring 'message' as an Outlook.MailItem but got
error 13 - Type mismatch.

Any ideas?

Dim myOlApp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim myItems As Outlook.Items
' Dim message as Outlook.MailItem

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
' Works if next line is commented out
Set myFolder = myFolder.Folders("Mail shot replies")
Set myItems = myFolder.Items

For Each message In myItems
' Next line gives run-time error 438
' "Object doesn't support this property or method"
MsgBox message.SenderEmailAddress, vbOKOnly
Next message
 
S

Sue Mosher [MVP-Outlook]

Probably because the item raising the error isn't a MailItem. Never assume; always check the Class. Thus:

Dim itm as Object
Dim message as MailItem

For Each itm In myItems
If itm.Class = olMail Then
Set message = itm
MsgBox message.SenderEmailAddress, vbOKOnly
End If
Next

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

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

Dmitry Streblechenko

A folder can have items other than the regular messages, like non-delivery
reports, meeting invitations, etc.
Declare message as a generic object, then check the if the message.Class
property is 43 before you access any MailItem specific properties.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

"Pete Dawson (Leeds Uni)" <Pete Dawson (Leeds
Uni)@discussions.microsoft.com> wrote in message
news:[email protected]...
 
P

Pete Dawson (Leeds Uni)

Thanks for the advice, Sue and Dmitry. The program now works OK.
 

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