Microsoft KB Archive/240935

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

Article ID: 240935

Article Last Modified on 6/30/2004

-

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 6.0 Enterprise Edition

-



This article was previously published under Q240935



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
 Start a new Standard EXE project in Visual Basic. Form1 is created by default. 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." Add a CommandButton to Form1 and change the Name property of the CommandButton to GetSelectedItem .  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  Press the F5 key to run the Project. Start Microsoft Outlook. Select any item (or number of items) in any Outlook folder.</li> Switch back to the running Visual Basic application and click the CommandButton.</li> Note that message boxes appear that display information about the selected item or items.</li></ol>

Keywords: kbhowto kboutlookobj KB240935

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.