Export (0) Print
Expand All

How to: Get and Log On to an Instance of Outlook

This topic shows how to obtain an Application object that represents an active instance of Microsoft 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.

MVP Icon

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

The following code examples 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 Primary Interop Assembly, 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(Object, Object, Object, Object) method of the NameSpace object to log on to the default profile, and returns that new instance of Outlook.

The following is the Visual Basic code example, followed by the C# code example.

If you use Microsoft Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 14.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#.

using Outlook = Microsoft.Office.Interop.Outlook;
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 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;
        }

    }
}

Other Resources

Show:
© 2014 Microsoft