We help IT Professionals succeed at work.

Reading Emails In Outlook

crcsupport
crcsupport used Ask the Experts™
on
I want to go over all emails in a folder. The attached code works for some emails and for some the type of myEmail is System._ComObject. Any ideas how to fix this ?

Thank you
For Each myEmail In mySubFolder.Items
  If TypeOf (myEmail) Is Microsoft.Office.Interop.Outlook.MailItem Then
          ' do some work
  End If
Next

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
What folder are you working with when this happens?

Author

Commented:
it is a subfolder in my mailbox. It has regular emails and undelivered email notifications.
Most Valuable Expert 2012
Top Expert 2014

Commented:
I would suspect those notifications are coming back as COM_Object.

Author

Commented:
yes, they are. Is there a way to read them ?
Most Valuable Expert 2012
Top Expert 2014
Commented:
Try

For Each myEmail In mySubFolder.Items
  If TypeOf (myEmail) Is Microsoft.Office.Interop.Outlook.MailItem Then
          ' do some work
  ElseIf TypeOf (myEmail) Is Microsoft.Office.Interop.Outlook.ReportItem Then
          ' do some other work
  End If
Next

Author

Commented:
looks like it worked ! Thank you
Most Valuable Expert 2012
Top Expert 2014

Commented:
Glad to help :-)