共用方式為


取得並登入 Outlook 的實例

本主題說明如何取得 Application 物件, 此物件代表在本機計算機上執行的使用中 Microsoft Outlook 實例,或是建立 Outlook 的新實例、登入預設配置檔,以及傳回該 Outlook 實例。

範例

注意事項

Helmut Obertanner 提供下列程式代碼範例。 Helmut 的專業知識適用於 Visual Studio 和 Outlook 的 Office 開發人員工具。

下列程式代碼範例包含 Sample 類別的 GetApplicationObject 方法,實作為 Outlook 載入宏專案的一部分。 每個項目都會新增以 Microsoft.Office.Interop.Outlook 命名空間為基礎的 Outlook 主要 Interop 元件參考。

GetApplicationObject 方法會使用 .NET Framework 類別庫中的類別來檢查並取得在本機計算機上執行的任何 Outlook 進程。 它會先使用 System.Diagnostics 命名空間中 Process 類別的 GetProcessesByName 方法,在共用進程名稱 “OUTLOOK” 的本機計算機上取得進程元件的數位。 為了檢查該陣列是否至少包含一個 Outlook 處理程序,GetApplicationObject 會使用 Microsoft Language Integrated Query (LINQ)。 System.Linq 命名空間中的 Enumerable 類別提供一組方法,包括實作 IEnumerable T> 泛型介面的<Count 方法。 由於 Array 類別會實作 IEnumerable (T) 介面,因此 GetApplicationObject 可以將 Count 方法套用至 GetProcessesByName 所傳回的數位,以查看是否有 Outlook 進程正在執行。 如果有,GetApplicationObject 會使用 System.Runtime.InteropServices 命名空間中 Marshal 類別的 GetActiveObject 方法來取得該 Outlook 實例,並將該對象轉換成 Outlook Application 物件。

如果 Outlook 未在本機計算機上執行,GetApplicationObject 會建立新的 Outlook 實例、使用 NameSpace 物件的 Logon (Object、Object、Object、Object) 方法來登入默認配置檔,並傳回該 Outlook 的新實例。

以下是 Visual Basic 程式代碼範例,後面接著 C# 程式代碼範例。

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.

Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Imports System.Diagnostics
Imports System.Linq
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports Outlook = Microsoft.Office.Interop.Outlook

Namespace OutlookAddIn2
    Class Sample

        Function GetApplicationObject() As Outlook.Application

            Dim application As Outlook.Application

            ' Check whether there is an Outlook process running.
            If Process.GetProcessesByName("OUTLOOK").Count() > 0 Then

                ' If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                application = DirectCast(Marshal.GetActiveObject("Outlook.Application"), Outlook.Application)
            Else

                ' If not, create a new instance of Outlook and sign in to the default profile.
                application = New Outlook.Application()
                Dim ns As Outlook.NameSpace = application.GetNamespace("MAPI")
                ns.Logon("", "", Missing.Value, Missing.Value)
                ns = Nothing
            End If

            ' Return the Outlook Application object.
            Return application
        End Function

    End Class
End Namespace
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookAddIn1
{
    class Sample
    {
        Outlook.Application GetApplicationObject()
        {

            Outlook.Application application = null;

            // Check whether there is an Outlook process running.
            if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
            {

                // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
            }
            else
            {

                // If not, create a new instance of Outlook and sign in to the default profile.
                application = new Outlook.Application();
                Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
                nameSpace.Logon("", "", Missing.Value, Missing.Value);
                nameSpace = null;
            }

            // Return the Outlook Application object.
            return application;
        }

    }
}

另請參閱