How to run Outlook using Process.Start(“outlook.ex

2019-08-30 18:11发布

My C# program needs to launch Office Outlook and get the current "running outlook application". In order to do that I've implemented the following simple program (so if you want you can test it simply):

using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;

static void Main(string[] args)
{
  Outlook.Application outlookObj = null;

  if (Process.GetProcessesByName("OUTLOOK").Count().Equals(0))
  {
    Process.Start("outlook.exe"); // MY PROGRAM STOPS HERE
  }
  var process = Process.GetProcessesByName("OUTLOOK").First();
  while (!process.HasExited)
  {
    try
    {
      outlookObj = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
      break;
    }
    catch
    {
      outlookObj = null;
    }
    System.Threading.Thread.Sleep(10);
  }

  string result = (outlookObj== null)? "DOES NOT WORK" : "OK";
  Console.WriteLine(result);
  Console.ReadLine();
}

My problem is that once Office Outlook starts running then my C# console application does not continue its job. After the Process.Start("outlook.exe"); instruction is executed then I must click on Visual Studio GUI in order to restart the console application and finally read "OK" on my console application.

How can I solve my problem?

4条回答
Animai°情兽
2楼-- · 2019-08-30 18:45

Microsoft wrote a example about how to log into a outlook instance. Although this is directly what you asked for in your question, the example contains how to start a new outlook application in the intended way

application = new Outlook.Application();

as a side note: in your example you use the following code:

 while (!process.HasExited)
  {
    try
    {
      outlookObj = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
      break;
    }
    catch
    {
      outlookObj = null;
    }
    System.Threading.Thread.Sleep(10);
  }

This is bad practice in your main thread as your applying 'busy waiting' by using the thread.sleep. This means you will 1. use CPU power while your application is doing nothing. 2. make your GUI completely unresponsive and if the thread.sleep is called to many times Windows will suggest to shut the process down (the whole screen gets white and eventually you get a popup asking you if you want to wait or just shut it down). There are plenty of ways in the .net framework to prevent both of these issues (for example using a waithandle, background worker or locking)

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-30 18:48

There is no need to run the a new process using the Process.Start method. Instead, you can add the Outlook reference to your C# project and create a new instance of the Application class. See C# app automates Outlook (CSAutomateOutlook) sample project for more information.

Also you may find the following articles helpful:

查看更多
乱世女痞
4楼-- · 2019-08-30 18:50

MAYBE the process need some time to start.

Try this:

  if (Process.GetProcessesByName("OUTLOOK").Count().Equals(0))
  {
    Process.Start("outlook.exe"); // MY PROGRAM STOPS HERE
  }

  while ((Process.GetProcessesByName("OUTLOOK").Count().Equals(0));

  var process = Process.GetProcessesByName("OUTLOOK").First();

This should cause starting process and waiting until it is avaible before trying to catch it...

查看更多
我想做一个坏孩纸
5楼-- · 2019-08-30 18:53

This works:

public static void StartOutlookIfNotRunning()
{
    string OutlookFilepath = @"C:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.EXE";
    if (Process.GetProcessesByName("OUTLOOK").Count() > 0) return;
    Process process = new Process();
    process.StartInfo = new ProcessStartInfo(OutlookFilepath);
    process.Start();
}
查看更多
登录 后发表回答