Reputation: 5557
I am attempting to utilize Office interop with C#, but I'm having some difficulties. Executing a test like the one I included below seems to work insofar as it launches Outlook and seems to connect with it. The issue is that if I then try to open the Outlook window (it starts hidden in the tray) I get an error message from Outlook saying The application was unable to start correctly (0xc0000142).
I do not get this error if Outlook was already running before I started my application. Am I doing something incorrectly or is something broken?
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookInteropTest1
{
class Program
{
static void Main(string[] args)
{
var app = new Outlook.Application();
Console.ReadKey();
}
}
}
Visual Studio Community 2017 Version 15.2
Office 360 - Outlook Version 1804 Build 9226.2156
Windows 10 Build 17115.1
EDIT: Tested this on Windows 7 and could not reproduce crash. I know that I had this working in Windows 10 at some point. I reinstalled my OS and it still crashes. I'm chocking this up to the typical Microsoft user experience unless anyone has any ideas on how to fix it.
Upvotes: 4
Views: 1258
Reputation: 130
I know this is old, but I was having the same issue and perhaps it will help someone in the future:
As IAmRajshah mentioned, only one istance of outlook can run, so, if Outlook is open your code olApp = new Outlook.Application();
will crash, you need to "connect" to the active instance of outlook with somenthing like this Oulook.Application olApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
The link below has a good example of this:
Get and sign in to an instance of Outlook
Upvotes: 1
Reputation: 979
Outlook is a singleton, so creating a new object will return the existing object if Outlook is already running.
In your case you also need to provide namespace to it
olApp = new Outlook.Application();
Outlook.Namespace ns = olApp.GetNamespace("MAPI");
ns.Logon();
Upvotes: 1