Partager via


How to: Obtain and Log On to an Instance of 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.

Ff869819.MVPLogo_Small_ZA10349011(en-us,office.14).jpg

Helmut Obertanner provided the following code samples. Helmut is a Microsoft Most Valuable Professional with expertise in Microsoft Office development tools in Microsoft Visual Studio and Microsoft Office Outlook. Helmut maintains a professional site at X4Uelectronix.

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 Microsoft Visual Studio and the Microsoft Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Microsoft Outlook 2010, ensure that you have installed the Outlook 2010 PIA and have added a reference to the Microsoft Outlook 14.0 Object Library component in Visual Studio. You should use the following code samples in the ThisAddIn class of a Visual Studio Tools for Office add-in for Outlook, such that the Application object in the code is a trusted Outlook Application object provided by ThisAddIn.Globals. For more information about using the Outlook PIA to develop managed Outlook solutions, see the Outlook 2010 Primary Interop Assembly Reference on MSDN.

The following code samples contain the GetApplicationObject method of the Sample class, implemented as part of an Outlook add-in project. Each project adds a reference to the Outlook PIA, which is based on the Microsoft.Office.Interop.Outlook namespace.

The GetApplicationObject method uses classes in the .NET Framework Class Library to check and obtain any Outlook process running on the local computer. 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". To check whether the array does contain at least one Outlook process, GetApplicationObject uses Microsoft Language Integrated Query (LINQ). The Enumerable class in the System.Linq namespace provides a set of methods, including the Count method, that implement the IEnumerable(T) generic interface. 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. 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.

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.

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; 
        } 
 
    } 
}

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