More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 24-May-2018 19:56

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Outlook and .NET Printer Friendly Version
Title: Undeliverable Bounce Back Message
Description: Hi Sue, Sorry to bug you but I'm having trouble with message Item in an Inbox which I am looping through. When I send a new message and the email address is incorrect. I get a bounce back message to the senders inbox. When i re-open the inbox (via my app) encounters an exception, see below. I'm pretty sure this is because the item is of a different type. Have you any ideas on being able to display this message in the Inbox via my app without the exception being encountered. Thanks, as always Steven
Date: 16-Jun-2006  09:31
Code level: intermediate
Code area: Outlook and .NET
Posted by: Steven Neild
This message is displayed as VB.NET
 This is the code:

        ' Create Outlook application.
        Dim oApp As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application
        ' Get Mapi NameSpace.
        Dim oNS As Microsoft.Office.Interop.Outlook.NameSpace = oApp.GetNamespace("mapi")
        ' Get Messages collection of Inbox.
        Dim myRecipient As Microsoft.Office.Interop.Outlook.Recipient
        myRecipient = oNS.CreateRecipient(System.Configuration.ConfigurationManager.AppSettings("MailboxName"))

        Dim oInbox As Microsoft.Office.Interop.Outlook.MAPIFolder = oNS.GetSharedDefaultFolder(myRecipient, Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
        oInboxItems = oInbox.Items

        'Variable to store sender email address
        Dim getEmailAddress As String
        ' Loop each inbox message.
        Dim oMsg As Microsoft.Office.Interop.Outlook.MailItem = Nothing
        Dim objSafeMail As Redemption.SafeMailItem = CreateObject("Redemption.SafeMailItem")
        objSafeMail.Item = oMsg
        Dim i As Integer

        For i = 1 To oInboxItems.Count   -------******Exception is thrown here

            oMsg = oInboxItems.Item(i)

This is the exception:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. 
This operation failed because the QueryInterface call on the COM component for the interface with IID 
'{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported 
(Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
All 9comments
Page [ 1  
  16-Jun-2006  09:57   
Most NDRs are ReportItem, not MailItem. The Inbox can also hold other types of items, such as meeting request responses. Therefore, you should NEVER assume that a mail folder contains only MailItem objects. Either Dim them as Object and check the Class property, which is common to all Outlook items, or use TypeOf to check the type of item before you access any item-specific properties or methods, e.g.

    If TypeOf oMsg Is Outlook.MailItem Then
            ' you know it's a MailItem

  30-Apr-2007  18:14   
I found that it helps to know what kind of type of items can appear is in a particular Exchange folder.

Task >
Contacts >
Journal >
Inbox >
Calendar >
Notes >
Misc >

  30-Apr-2007  21:54   
Oh I forget to mention

For exchange public folders or more generally speaking newsgroups
  02-May-2007  15:06   
Actually, Art, any Outlook item can appear in any folder. You should never assume that a folder contains items only of one type.
  14-Aug-2007  09:13   
I have a similar problem, I tried you sugestion, but it didnt work:

Dim oApp As Microsoft.Office.Interop.Outlook.Application = New Microsoft.Office.Interop.Outlook.Application()
        Dim oNS As Microsoft.Office.Interop.Outlook.NameSpace = oApp.GetNamespace("mapi")
        Dim oInbox As Microsoft.Office.Interop.Outlook.MAPIFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
        Dim oItems As Microsoft.Office.Interop.Outlook.Items = oInbox.Items

        ' Dim mail As Microsoft.Office.Interop.Outlook.MailItem
        Dim mail As Object

        For i As Integer = 1 To oItems.Count

            mail = oItems.Item(i)
            If (TypeOf oItems.Item(i) Is Microsoft.Office.Interop.Outlook.MailItem) Then
                If (mail.SenderName = "System Administrator") Then
                End If
            End If

i have the first 8 or 9 items in my inbox are emails, but it never passes the type of check

.... thnx
  14-Aug-2007  09:14   
i also tried doing the type of on mail
  14-Aug-2007  09:51   
i also tried doing the type of on mail, sometimes it passes the check, sometimes it doesnt, by this i mean i stop the program, start it and it works, then i stop it, start it and it doesnt pass the type of check
  10-Sep-2007  08:39   
Try the logic this way, Paul:

Dim mail As Microsoft.Office.Interop.Outlook.MailItem
Debug.Print oItems.Item(i).Subject.ToString
If (TypeOf oItems.Item(i) Is Microsoft.Office.Interop.Outlook.MailItem) Then
    mail = oItems.Item(i)

Remember that "first" may mean something different to Outlook than to the user looking at the items in a folder, unless you use Items.Sort to put them in a particular order for processing.
  22-Jun-2009  05:06   
Hi, I have the same problem. But My project is in C#. And there are not item collection of oItems (oInbox.Items). So please give a solution for that
Page [ 1