How To Get the Currently Selected Item in an Outlook Folder from Visual Basic

Article translations Article translations
Article ID: 240935 - View products that this article applies to.
This article was previously published under Q240935
Expand all | Collapse all

On This Page

SUMMARY

Although it is rather simple to change the selected item in an Outlook folder programmatically, Outlook 97 and 98 did not expose a method for retrieving the currently selected item in a folder. Outlook 2000, however, exposes a new interface for a Selection object.

MORE INFORMATION

The following sample illustrates how you can automate Outlook to retrieve information about the currently selected item in a folder using the Selection object.

Sample

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. In the Visual Basic editor environment, click the ProjectCommandBar, click References, scroll down the list of references and select "Microsoft Outlook 9.0 Object Library."
  3. Add a CommandButton to Form1 and change the Name property of the CommandButton to GetSelectedItem.
  4. Add the following code to the Form1 module:
    Private Sub GetSelectedItem_Click()
        ' This uses an existing instance if available (default Outlook behavior).
        Dim oApp As New Outlook.Application
        Dim oExp As Outlook.Explorer
        Dim oSel As Outlook.Selection   ' You need a selection object for getting the selection.
        Dim oItem As Object             ' You don't know the type yet.
        
        Set oExp = oApp.ActiveExplorer  ' Get the ActiveExplorer.
        Set oSel = oExp.Selection       ' Get the selection.
        
        For i = 1 To oSel.Count         ' Loop through all the currently .selected items
            Set oItem = oSel.Item(i)    ' Get a selected item.
            DisplayInfo oItem           ' Display information about it.
        Next i
    End Sub
    
    Sub DisplayInfo(oItem As Object)
        
        Dim strMessageClass As String
        Dim oAppointItem As Outlook.AppointmentItem
        Dim oContactItem As Outlook.ContactItem
        Dim oMailItem As Outlook.MailItem
        Dim oJournalItem As Outlook.JournalItem
        Dim oNoteItem As Outlook.NoteItem
        Dim oTaskItem As Outlook.TaskItem
        
        ' You need the message class to determine the type.
        strMessageClass = oItem.MessageClass
        
        If (strMessageClass = "IPM.Appointment") Then       ' Calendar Entry.
            Set oAppointItem = oItem
            MsgBox oAppointItem.Subject
            MsgBox oAppointItem.Start
        ElseIf (strMessageClass = "IPM.Contact") Then       ' Contact Entry.
            Set oContactItem = oItem
            MsgBox oContactItem.FullName
            MsgBox oContactItem.Email1Address
        ElseIf (strMessageClass = "IPM.Note") Then          ' Mail Entry.
            Set oMailItem = oItem
            MsgBox oMailItem.Subject
            MsgBox oMailItem.Body
        ElseIf (strMessageClass = "IPM.Activity") Then      ' Journal Entry.
            Set oJournalItem = oItem
            MsgBox oJournalItem.Subject
            MsgBox oJournalItem.Actions
        ElseIf (strMessageClass = "IPM.StickyNote") Then    ' Notes Entry.
            Set oNoteItem = oItem
            MsgBox oNoteItem.Subject
            MsgBox oNoteItem.Body
        ElseIf (strMessageClass = "IPM.Task") Then          ' Tasks Entry.
            Set oTaskItem = oItem
            MsgBox oTaskItem.DueDate
            MsgBox oTaskItem.PercentComplete
        End If
        
    End Sub
    					
  5. Press the F5 key to run the Project.
  6. Start Microsoft Outlook.
  7. Select any item (or number of items) in any Outlook folder.
  8. Switch back to the running Visual Basic application and click the CommandButton.
  9. Note that message boxes appear that display information about the selected item or items.

Properties

Article ID: 240935 - Last Review: June 30, 2004 - Revision: 2.2
APPLIES TO
  • Microsoft Outlook 2000 Standard Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
Keywords: 
kbhowto kboutlookobj KB240935

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com