Поделиться через


Практическое руководство. Определение текущего элемента Outlook

Обновлен: Ноябрь 2007

Применение

Сведения, приведенные в данном разделе, относятся только к указанным проектам Visual Studio Tools for Office и версиям Microsoft Office.

Тип проекта

  • Проекты уровня приложения

Версия Microsoft Office

  • Outlook 2003

  • Outlook 2007

Дополнительные сведения см. в разделе Доступность функций по типам приложений и проектов.

В данном примере с помощью события Explorer.SelectionChange отображается имя текущей папки и некоторая информация о выбранном элементе. Затем с помощью кода отображается выбранный элемент.

Пример

Dim WithEvents currentExplorer As Outlook.Explorer = Nothing

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
     ByVal e As System.EventArgs) Handles Me.Startup
    currentExplorer = Me.Application.Explorers.Application.ActiveExplorer
    AddHandler currentExplorer.SelectionChange, AddressOf _
        currentExplorer_Event
End Sub

Public Sub currentExplorer_Event()
    Dim selectedFolder As Outlook.MAPIFolder = _
        Me.Application.ActiveExplorer().CurrentFolder
    Dim expMessage As String = "Your current folder is " _
        & selectedFolder.Name & "." & vbCrLf
    Dim itemMessage As String = "Item is unknown."
    Try
        If Me.Application.ActiveExplorer.Selection.Count > 0 Then
            Dim selObject As Object = Me.Application.ActiveExplorer _
                .Selection.Item(1)
            If (TypeOf selObject Is Outlook.MailItem) Then
                Dim mailItem As Outlook.MailItem = _
                    TryCast(selObject, Outlook.MailItem)
                itemMessage = "The item is an e-mail message." & _
                    " The subject is " & mailItem.Subject & "."
                mailItem.Display(False)
            ElseIf (TypeOf selObject Is Outlook.ContactItem) Then
                Dim contactItem As Outlook.ContactItem = _
                   TryCast(selObject, Outlook.ContactItem)
                itemMessage = "The item is a contact." & _
                    " The full name is " & _
                    contactItem.Subject & "."
                contactItem.Display(False)
            ElseIf (TypeOf selObject Is Outlook. _
                AppointmentItem) Then
                Dim apptItem As Outlook.AppointmentItem = _
                   TryCast(selObject, Outlook.AppointmentItem)
                itemMessage = "The item is an appointment." _
                    & apptItem.Subject & "."
            ElseIf (TypeOf selObject Is Outlook.TaskItem) Then
                Dim taskItem As Outlook.TaskItem = _
                    TryCast(selObject, Outlook.TaskItem)
                itemMessage = "The item is a task." & _
                    " The body is " & taskItem.Body & "."
            ElseIf (TypeOf selObject Is Outlook.MeetingItem) Then
                Dim meetingItem As Outlook.MeetingItem = _
                    TryCast(selObject, Outlook.MeetingItem)
                itemMessage = "The item is a meeting item. " & _
                    "The subject is " & meetingItem.Subject & "."
            End If
        End If
        expMessage = expMessage & itemMessage
    Catch ex As Exception
        expMessage = ex.Message
    End Try
    MessageBox.Show(expMessage)
End Sub
Outlook.Explorer currentExplorer = null;

private void ThisAddIn_Startup
    (object sender, System.EventArgs e)
{
    currentExplorer = this.Application.ActiveExplorer();
    currentExplorer.SelectionChange += new Outlook
        .ExplorerEvents_10_SelectionChangeEventHandler
        (CurrentExplorer_Event);
}

private void CurrentExplorer_Event()
{ 
    Outlook.MAPIFolder selectedFolder = 
        this.Application.ActiveExplorer().CurrentFolder;
    String expMessage = "Your current folder is " 
        + selectedFolder.Name + ".\n";
    String itemMessage = "Item is unknown.";
    try
    {
        if (this.Application.ActiveExplorer().Selection.Count > 0)
        {
            Object selObject = this.Application.ActiveExplorer().Selection[1];
            if (selObject is Outlook.MailItem)
            {
                Outlook.MailItem mailItem = 
                    (selObject as Outlook.MailItem);
                itemMessage ="The item is an e-mail message." +
                    " The subject is " + mailItem.Subject + ".";
                mailItem.Display(false);
            }
            else if (selObject is Outlook.ContactItem)
            {
                Outlook.ContactItem contactItem = 
                    (selObject as Outlook.ContactItem);
                itemMessage = "The item is a contact." + 
                    " The full name is " + contactItem.Subject + ".";
                contactItem.Display(false);
            }
            else if (selObject is Outlook.AppointmentItem)
            {
                Outlook.AppointmentItem apptItem = 
                    (selObject as Outlook.AppointmentItem);
                itemMessage = "The item is an appointment." + 
                    " The subject is " + apptItem.Subject + ".";
            }
            else if (selObject is Outlook.TaskItem)
            {
                Outlook.TaskItem taskItem = 
                    (selObject as Outlook.TaskItem);
                itemMessage = "The item is a task. The body is " 
                    + taskItem.Body + ".";
            }
            else if (selObject is Outlook.MeetingItem)
            {
                Outlook.MeetingItem meetingItem =
                    (selObject as Outlook.MeetingItem);
                itemMessage = "The item is a meeting item. " +
                     "The subject is " + meetingItem.Subject + ".";
            }
        }
    expMessage = expMessage + itemMessage;
    }
    catch (Exception ex)
    {
    expMessage = ex.Message;
    }
    MessageBox.Show(expMessage);
}

Компиляция кода

Для этого примера необходимы:

  • Элементы встреч, контактов и электронной почты в Microsoft Office Outlook.

См. также

Задачи

Практическое руководство. Извлечение папки по названию

Практическое руководство. Поиск определенного контакта

Основные понятия

Общие сведения об объектной модели Outlook