Using Outlook Objects

Team Fly 

Page 109

Using Outlook Objects

One quick way to add an e-mail-reading feature to a VB.NET application is to employ the facilities in Outlook.

First, use VB.NET's Project image Add Reference dialog box to add a reference to the COM library named Microsoft Outlook 10.0 Object Library. (Your version might not be 10.0.)

The Outlook object hierarchy exposes several ''folder" objects: olFolderCalendar, olFolderContacts, olFolderDeletedItems, olFolderDrafts, olFolderInbox, olFolderJournal, olFolderNotes, olFolderOutbox, olFolderSentMail, olFolderTask. If you've worked with Outlook, you'll notice that these folders correspond to the various utilities and features available within Outlook.

To access the Outlook folders, you have to instantiate them indirectly by creating a MAPI message store (like API, only with the term messaging prepended).

Don't bother yourself with the wearisome nonsense that "explains" why you must first create a special Outlook namespace, then get a MAPI store. You'll add nothing to your understanding of programming by trying to follow the reasoning for this unique way of accessing Outlook's object library. Only Outlook does things this freaky way, so no use learning a process that occurs only once. You wouldn't study mammalian behavior by examining a platypus. Just copy the code in the examples below and you're home free.

WARNING Before you can test this example code, you must ensure that there's at least one e-mail message in your Outlook Inbox. Otherwise, the code will fail—I'm not bothering here to employ a Try...Catch...End Try failsafe error-trapping structure.

Put a TextBox, ListBox, and button on a form, then type Listing 4.13 into the Button's Click event.

LISTING 4.13: GETTING INBOX E-MAIL MESSAGES

Private Sub Button1_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles Button1.Click

        Dim o As New Outlook.Application
        Dim MAPI As Outlook.MAPIFolder
        Dim NSpace As Outlook.NameSpace = o.GetNamespace("MAPI")

        MAPI = NSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        Dim it As Outlook._Items = MAPI.Items

        Dim i As Integer, s As String
        Dim m As Outlook.MailItem


        m = it.Item(1)
        s = m.SenderName & ": "& m.Subject 'get the name and topic
        ListBox1.Items.Add(s)
        TextBox1.Text = m.Body 'send the actual message to the TextBox

End Sub

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net