How to inspect Inbox messages with VBA?



I use the following to loop through email messages in the Outlook Inbox:

Dim olmi as Outlook.MailItem
Dim olfi as Outlook.Items
Dim olr As Outlook.Recipient
Dim olrs As Outlook.Recipients

Set olfi = olFolder.Items

For Each olmi In olfi
'match recipient address
Set olrs = olmi.Recipients
For Each olr In olrs 'check every recipient the message was sent to
Debug.Print "checking olr.Address [" & olr.Address & "]"
If olr.Address = strAddress And olmi.SenderEmailAddress <>
olr.Address Then
Debug.Print "Found matching Recipient address!"
End If
End If

The problem is when users have encrypted messages, or messages of SYSTEM
type (e.g. an undeliverable alert from the System Administrator) in their
Inbox. When one of these messages is encountered, a Type Mismatch error is
returned and the code exits the loop. I've tried a On Error Resume Next,
but no luck.

Is there a way to test for encrypted or SYSTEM type messages? It appears
these types of messages are not Outlook.MailItems and therefore the error
occurs as soon as the "For Each olmi In olfi" line is executed. I'm
thinking I could insert a line of code above that For Each statement to test
if the message can or cannot be inspected... ? Other options?

Thanks in advance.

Ken Slovak - [MVP - Outlook]

You can check for item.Class or item.MessageClass. For Class a mail item
would return olMail.

You can do that by declaring your mail item as Object and skipping the
processing if Class <> olMail. Of course that would need to late bind any
properties of the Object. You could assign a MailItem from the Object if the
test passes.

Signed or encrypted items might return olMail but their MessageClass would
not be "IPM.Note" but something else, perhaps .Note.something or some custom
message class.

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