In order to start outlook and wait until it's available via Interop I wrote the following Console app:
static Outlook.Application outlook;
static void Main(string[] args)
{
Console.WriteLine("Enter Profile:");
var profile = Console.ReadLine();
var process = Process.GetProcessesByName("Outlook").FirstOrDefault();
if (process == null)
{
ProcessStartInfo startInfo = new ProcessStartInfo("outlook.exe", $"/profile \"{profile}\""); // MK_E_UNAVAILABLE appears until I click in the console window
// ProcessStartInfo startInfo = new ProcessStartInfo("outlook.exe"); // Works as expected
process = Process.Start(startInfo);
}
while (outlook == null && !process.HasExited)
{
try
{
outlook = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
break;
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
outlook = null;
}
process.Refresh();
Thread.Sleep(500);
}
if (outlook != null)
Console.WriteLine($"Outlook is running.");
Console.ReadKey();
}
I verified in task manager that console app is running under the same user as Outlook does (see here).
The output of the console app is:
Vorgang nicht verfügbar. (Ausnahme von HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))
Vorgang nicht verfügbar. (Ausnahme von HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))
Vorgang nicht verfügbar. (Ausnahme von HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))
This message gets repeated until I click into the console window or resize it. Then output changes to
Outlook is running.
This does only happen when starting outlook with the profile parameter. Without profile parameter the message Outlook is running.
appears straight after I selected the profile in the Outlook profile dialog.
Can anybody explain what's the reason for this behaviour?