获取并登录 Outlook 实例Obtain and Log On to an Instance of Outlook

本主题介绍如何获取代表活动 Outlook 实例的 Application 对象(如果该实例在本地计算机上运行)或创建新的 Outlook 实例,登录到默认配置文件并返回该 Outlook 实例。This topic shows how to obtain an Application object that represents an active instance of Outlook, if there is one running on the local computer, or to create a new instance of Outlook, log on to the default profile, and return that instance of Outlook.

[!注释] Helmut Obertanner 提供了以下代码示例。Helmut Obertanner provided the following code samples. Helmut 是 Microsoft 最有价值的专家 ,拥有 Microsoft Visual Studio Tools for Office 和 Microsoft Office Outlook 中的 Microsoft Office 开发工具方面的专业知识。Helmut is a Microsoft Most Valuable Professional with expertise in Microsoft Office development tools in Microsoft Visual Studio and Microsoft Office Outlook.

下面的托管代码示例是使用 C# 和 Visual Basic 编写的。The following managed code samples are written in C# and Visual Basic. 若要运行需调入组件对象模型 (COM) 的 .NET Framework 托管代码示例,您必须使用可定义托管接口并将其映射到对象模型类型库中的 COM 对象的互操作程序集。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.

对于 Outlook,您可以使用 Visual Studio 和 Outlook 主互操作程序集 (PIA)。For Outlook, you can use Visual Studio and the Outlook Primary Interop Assembly (PIA). 在您运行适用于 Outlook 2013 的托管代码示例之前,请确保您已安装了 Outlook 2013 PIA 并且已添加了对 Visual Studio 中的 Microsoft Outlook 15.0 对象库组件的引用。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.

您应该使用 Outlook 加载项的 ThisAddIn 类中的以下代码示例(使用 Visual Studio Office 开发人员工具)。You should use the following code samples in the ThisAddIn class of an Outlook add-in (using Office Developer Tools for Visual Studio). 代码中的 应用程序对象必须是由 **** 提供的受信任 Outlook ThisAddIn.Globals对象。The Application object in the code must be a trusted Outlook Application object provided by ThisAddIn.Globals. 有关使用 Outlook PIA 开发托管 Outlook 解决方案的详细信息, 请参阅 MSDN 上的 "欢迎使用 outlook 主互操作程序集参考"。For more information about using the Outlook PIA to develop managed Outlook solutions, see the Welcome to the Outlook Primary Interop Assembly Reference on MSDN.

下面的代码示例包含GetApplicationObject Sample类的方法, 该方法作为 Outlook 外接程序项目的一部分实现。The following code samples contain the GetApplicationObject method of the Sample class, implemented as part of an Outlook add-in project. 每个项目添加到 Outlook PIA,基于 Microsoft.Office.Interop.Outlook命名空间的引用。Each project adds a reference to the Outlook PIA, which is based on the Microsoft.Office.Interop.Outlook namespace.

GetApplicationObject方法使用 .net Framework 类库中的类检查和获取在本地计算机上运行的任何 Outlook 进程。The GetApplicationObject method uses classes in the .NET Framework Class Library to check and obtain any Outlook process running on the local computer. 它首先使用 GetProcessesByName 命名空间中的process类的**** 方法来获取本地计算机上共享进程名称 "OUTLOOK" 的进程组件数组。 ****It first uses the GetProcessesByName method of the Process class in the System.Diagnostics namespace to obtain an array of process components on the local computer that share the process name "OUTLOOK".

若要检查数组是否至少包含一个 Outlook 进程, 请GetApplicationObject使用 Microsoft 语言集成查询 (LINQ)。To check whether the array does contain at least one Outlook process, GetApplicationObject uses Microsoft Language Integrated Query (LINQ). 系统中的可**枚举** 类**。 Linq** 命名空间提供一组方法, 其中包括实现**IEnumerable (T)** 泛型接口的**Count** 方法。The Enumerable class in the System.Linq namespace provides a set of methods, including the Count method, that implement the IEnumerable(T) generic interface.

由于**Array** 类实现IEnumerable (T) 接口, GetApplicationObject因此可以将Count方法应用于由GetProcessesByName返回的数组, 以查看是否有 Outlook 进程正在运行。Because the Array class implements the IEnumerable(T) interface, GetApplicationObject can apply the Count method to the array returned by GetProcessesByName to see whether there is an Outlook process running. 如果GetApplicationObject有, 则使用**InteropServices** 命名空间中的**Marshal** 类的**GetActiveObject** 方法来获取该 outlook 实例, 并将该对象强制转换为 outlook应用程序对象。If there is, GetApplicationObject uses the GetActiveObject method of the Marshal class in the System.Runtime.InteropServices namespace to obtain that instance of Outlook, and casts that object to an Outlook Application object.

如果 outlook 未在本地计算机上运行, GetApplicationObject则创建新的 outlook 实例, 使用**NameSpace** 对象的**Logon** 方法登录到默认配置文件, 并返回该新的 outlook 实例。If Outlook is not running on the local computer, GetApplicationObject creates a new instance of Outlook, uses the Logon method of the NameSpace object to log on to the default profile, and returns that new instance of Outlook.

以下是 C# 代码示例。The following is the C# code sample.

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 代码示例。The following is the Visual Basic code sample.

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

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.