A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

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



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old March 2nd 06, 05:30 PM posted to microsoft.public.outlook.program_vba
Pete Dawson (Leeds Uni)
external usenet poster
 
Posts: 1
Default Why do I get a type mismatch when trying to access mail items?

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

Ads
  #2  
Old March 2nd 06, 05:55 PM posted to microsoft.public.outlook.program_vba
Sue Mosher [MVP-Outlook]
external usenet poster
 
Posts: 11,651
Default Why do I get a type mismatch when trying to access mail items?

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
http://www.turtleflock.com/olconfig/index.htm
and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx


"Pete Dawson (Leeds Uni)" Pete Dawson (Leeds wrote in message ...
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

  #3  
Old March 2nd 06, 05:57 PM posted to microsoft.public.outlook.program_vba
Dmitry Streblechenko
external usenet poster
 
Posts: 2,116
Default Why do I get a type mismatch when trying to access mail items?

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
wrote in message
...
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



  #4  
Old March 3rd 06, 01:16 PM posted to microsoft.public.outlook.program_vba
Pete Dawson (Leeds Uni)
external usenet poster
 
Posts: 1
Default Why do I get a type mismatch when trying to access mail items?

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

"Sue Mosher [MVP-Outlook]" wrote:

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
http://www.turtleflock.com/olconfig/index.htm
and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx


"Pete Dawson (Leeds Uni)" Pete Dawson (Leeds wrote in message ...
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


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The current document type cannot be sent as mail Teddy Banks Outlook Express 12 March 31st 06 02:20 AM
outlook mail reply window shows very small type AnthonyD-B Outlook - General Queries 2 February 8th 06 10:42 AM
how do i find type of mail opened? Sue Mosher [MVP-Outlook] Outlook - Using Forms 0 February 6th 06 08:13 PM
Moving a Mix of Mail Items and Report Items Steve Roberts Outlook and VBA 1 January 24th 06 10:36 PM
Why is "FAX" a valid e-mail type in address book? Opus Outlook - Using Contacts 3 January 13th 06 08:24 PM


All times are GMT +1. The time now is 04:09 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.