VBA Iterate through Outlook Folder Type Mismatch Outlook 2003

R

Rog

Outlook 2003, Folder is created in myown mailbox.
Running in Access 2003 VBA environment. I get a Type
mismatch, but don't understand why?

Dim myOlapp As Outlook.Application
Dim myoMsg As Outlook.MailItem
Dim myOFolder As Outlook.MAPIFolder
Dim myAttachment As Attachment
Dim sTemp As String
Dim sSubject As String
Dim sEmailAddress As String
Dim iFileNum As Long
Dim sFile As String
Dim sFileTemp As String
Dim sSql As String
Dim i As Long

Set myOlapp = New Outlook.Application
Set myOFolder = myOlapp.Session.PickFolder

For Each myoMsg In myOFolder.Items -- Error happens
here on first message.
ssubject = myomsg.subject
next

Funny thing is if I go to Command Window I can do the
following while stopped on this line...

myOfolder.items(i).Subject and get a value back
 
K

Ken Slovak - [MVP - Outlook]

If the first item isn't a mail item you get that error. Does it work
if you declare it as Object?
 
R

Rog

No error message when declaring it as as object.

When I declare it as an object, what methods/properties
will I be allowed to execute on that object? How can I
tell what type of object it is?

Thanks,

Rog
 
K

Ken Slovak - [MVP - Outlook]

Declaring it as Object just late binds it, so the code will execute
marginally slower. You can use any valid property of the item, you
just won't get the intellisense features when you develop your code.

That was mostly a test. It looks like the first item you are picking
up isn't a mail item, which would have caused your error. A folder
like the Inbox can hold items of different types, not all are emails.
A contacts folder can hold not only contacts but also personal
distribution lists. So some sort of sanity check for what type of item
you are getting is needed, you can't just assume a specific item type.

You can just work with the Object, you can declare a MailItem and use
error handling to see if the assignment causes an error and if so
clear the error and proceed to the next item, or you can test the
Object and if it's a MailItem you can then assign the Object to a
MailItem.

To test an item to see if it's a mail item you can use a few different
properties:
..Class = olMail (43)
InStr(1, .MessageClass, "IPM.Note") > 0
TypeName(Object) = "MailItem"
 

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