HOWTO: Get the Currently Selected Item in an Outlook Folder from Visual Basic
ID: Q240935
|
The information in this article applies to:
-
Microsoft Outlook 2000
-
Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 5.0, 6.0
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.
- Switch back to the running Visual Basic application and click the CommandButton.
- Note that message boxes appear that display information about the selected item or items.
Additional query words:
Keywords : kbOutlook kbOutlookObj kbVBp500 kbVBp600 kbGrpDSO kbDSupport kboutlook2000
Version : WINDOWS:2000,5.0,6.0
Platform : WINDOWS
Issue type : kbhowto