How to inspect items in OL folder using VBA?

D

deko

I need to loop through the Outlook Inbox (or other OL folder) and inspect
the recipient address of each message. The problem is when an encrypted or
"undeliverable message" is encountered in the folder - a Type Mismatch error
is returned. Is there a way to identify these non-MailItem messages and
skip over them?

Here's my code (using automation from Access):

Dim olapp as Outlook.Application
Dim olmi as Outlook.MailItem
Dim olr As Outlook.Recipient
Dim olrs As Outlook.Recipients
Dim olFolder As Outlook.MAPIFolder
Dim olfi As Outlook.Items
Dim olns As Outlook.NameSpace

Set olapp = New Outlook.Application
Set olns = olapp.GetNamespace("MAPI")
Set olFolder = olns.PickFolder
Set olfi = olFolder.Items

I want to inspect each olmi in olfi. The problem is there are sometimes
items in the olfi collection that are not olmi's - e.g. ReportItems
(undeliverable messages), and encrypted messages (not sure what these are).

Normally, I can just loop like this:

For Each olmi In olfi
'match recipient address with strAddress
Set olrs = olmi.Recipients
For Each olr In olrs 'check every recipient the message was sent to
If olr.Address = strAddress Then
Debug.Print "Found matching Recipient address!"
End If
Next
Next

But if a non-olmi item is encountered, I get a Type Mismatch error. Is
there a way to test to make sure the item is an olmi before entering the For
Each loop? In pseudo code, it might look something like this:

For Each item In olfi
If item <> olmi then
GoTo Next_Item
Else
Set olrs = olmi.Recipients
For Each olr In olrs 'check every recipient the message was sent
to
If olr.Address = strAddress Then
Debug.Print "Found matching Recipient address!"
End If
Next
End if
Next_Item:
Next

Other suggestions?

Thanks in advance.
 
M

Michael Bauer

Am Thu, 29 Dec 2005 19:58:17 -0800 schrieb deko:


Instead of olmi in the For Each loop use an as Object declared variable.
Then check its type with
If TypeOf obj Is Outlook.MailItem Then
before assigning it to olmi.
 
D

deko

Instead of olmi in the For Each loop use an as Object declared variable.
Then check its type with
If TypeOf obj Is Outlook.MailItem Then
before assigning it to olmi.

Works like a champ! Thanks.

Dim olapp as Outlook.Application
Dim olmi as Outlook.MailItem
Dim olr As Outlook.Recipient
Dim olrs As Outlook.Recipients
Dim olFolder As Outlook.MAPIFolder
Dim olfi As Outlook.Items
Dim olns As Outlook.NameSpace
Dim obj Item as Object

Set olapp = New Outlook.Application
Set olns = olapp.GetNamespace("MAPI")
Set olFolder = olns.PickFolder
Set olfi = olFolder.Items

For Each objItem In olfi
If TypeOf objItem Is Outlook.MailItem Then
Set olmi = objItem
Set olrs = olmi.Recipients
For Each olr In olrs
If olr.Address = strAddress Then
Debug.Print "Found matching Recipient address!"
End If
Next
End If
Next
 

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