共用方式為


取得並登入 Outlook 的實例

本主題顯示如何取得代表 Outlook 的使用中執行個體 (如果本機電腦上有執行這種執行個體的話) 的 Application 物件,或建立 Outlook 的新執行個體,登入預設設定檔,及傳回該 Outlook 執行個體。

Helmut Obertanner 提供了下列程式碼範例。 Helmut 是 Microsoft 最有價值專家 ,具備 Microsoft Visual Studio 和 Microsoft Office Outlook 中 Microsoft Office 開發工具的專業知識。

The following managed code samples are written in C# and Visual Basic. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library.

For Outlook, you can use Visual Studio and the Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2013, ensure that you have installed the Outlook 2013 PIA and have added a reference to the Microsoft Outlook 15.0 Object Library component in Visual Studio.

您應該使用 Office Developer Tools for Visual Studio) ,在 Outlook 增益集 (類別中使用下列程式碼 ThisAddIn 範例。 程式碼中的Application物件必須是 所 ThisAddIn.Globals 提供的受信任 Outlook應用程式物件。 如需使用 Outlook PIA 開發受控 Outlook 解決方案的詳細資訊,請參 閱 MSDN 上的歡迎使用 Outlook 主要 Interop 元件參考

下列程式碼範例包含 GetApplicationObject 類別的 Sample 方法,實作為 Outlook 增益集專案的一部分。 每個專案都會新增 Outlook PIA 的參考,以 Microsoft.Office.Interop.Outlook 命名空間為基礎。

方法 GetApplicationObject 會使用 .NET Framework 類別庫中的類別來檢查並取得在本機電腦上執行的任何 Outlook 進程。 它會先使用System.Diagnostics命名空間中Process類別的GetProcessesByName方法,在共用進程名稱 「OUTLOOK」 的本機電腦上取得進程元件的陣列。

若要檢查陣列是否至少包含一個 Outlook 程式,請 GetApplicationObject 使用 Microsoft Language Integrated Query (LINQ) 。 System.Linq命名空間中的Enumerable類別提供一組方法,包括Count方法,可實作IEnumerable (T) 泛型介面。

由於 Array 類別會實作 IEnumerable (T) 介面, GetApplicationObject 因此可以將 Count 方法套用至 GetProcessesByName 所傳回的陣列,以查看是否有 Outlook 進程正在執行。 如果有, GetApplicationObject 請使用System.Runtime.InteropServices命名空間中Marshal類別的GetActiveObject方法來取得該 Outlook 實例,並將該物件轉換成 Outlook Application物件。

如果 Outlook 未在本機電腦上執行, GetApplicationObject 請建立 Outlook 的新實例、使用NameSpace物件的Logon方法登入預設設定檔,並傳回該 Outlook 的新實例。

下列為 C# 程式碼範例。

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 if 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 log on 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; 
        } 
 
    } 
}

下列為 Visual Basic 程式碼範例。

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 if 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 log on 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

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應