How to: Obtain and Log On to an Instance of Outlook
Outlook Developer Reference |
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 provided the following code samples. Helmut is a Microsoft Most Valuable Professional with expertise in Microsoft Visual Studio Tools for Office and Microsoft Office Outlook. Helmut maintains a professional site at http://www.outlooksharp.de/. |
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 Office Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2007, ensure that you have installed the Outlook 2007 PIA and have added a reference to the Microsoft Outlook 12.0 Object Library component in Visual Studio. For more information about using the Outlook PIA to develop managed Outlook solutions, see Welcome to the Outlook 2007 Primary Interop Assembly Reference.
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.
|
The following is the Visual Basic code sample.
|