Microsoft KB Archive/313795

From BetaArchive Wiki
Knowledge Base


Article ID: 313795

Article Last Modified on 5/20/2005



APPLIES TO

  • Microsoft Office Outlook 2003
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition



This article was previously published under Q313795

For a Microsoft Visual Basic 6.0 version of this article, see 171603.

IN THIS TASK

SUMMARY

SUMMARY

This article describes how to use Microsoft Outlook 10.0 Object Library to retrieve unread messages from the Outlook Inbox in Visual Basic .NET.

back to the top

Create Sample to Retrieve Unread Messages from the Inbox

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click Console Application under Templates. By default, Module1.vb is created.
  4. Add a reference to the Microsoft Outlook 10.0 Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab.
    3. Click Microsoft Outlook 10.0 Object Library, and then click Select
    4. Click OK. If you are prompted to generate wrappers for the library that you selected, click Yes.
  5. In the Code window, replace the default code with the following code:

    Imports System.Reflection
    
    Module Module1
    
        Sub Main()
           ' Create Outlook application.
            Dim oApp As Outlook.Application = New Outlook.Application()
            
            ' Get Mapi NameSpace.
            Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
            oNS.Logon("YourValidProfile", Missing.Value, False, True) ' TODO:
    
            ' Get Messages collection of Inbox.
            Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
            Dim oItems As Outlook.Items = oInbox.Items
            Console.WriteLine("Total : " & oItems.Count)
    
            ' Get unread e-mail messages.
            oItems = oItems.Restrict("[Unread] = true")
            Console.WriteLine("Total Unread : " & oItems.Count)
    
            ' Loop each unread message.
            Dim oMsg As Outlook.MailItem
            Dim i As Integer
    
            For i = 1 To oItems.Count
                oMsg = oItems.Item(i)
    
                Console.WriteLine(i)
                Console.WriteLine(oMsg.SenderName)
                Console.WriteLine(oMsg.Subject)
                Console.WriteLine(oMsg.ReceivedTime)
                Console.WriteLine(oMsg.Body)
                Console.WriteLine("---------------------------")
            Next
    
            ' Log off.
            oNS.Logoff()
    
            ' Clean up.
            oApp = Nothing
            oNS = Nothing
            oItems = Nothing
            oMsg = Nothing
        End Sub
    
    End Module
                        
  6. Modify the code where you see the TODO comments.
  7. Press F5 to build and to run the application.
  8. Verify that the unread messages are retrieved.

back to the top


Additional query words: OOM

Keywords: kbhowtomaster KB313795