How to run Outlook using Process.Start(“outlook.exe”) and get the control back

两盒软妹~` 提交于 2019-12-12 04:49:12

问题


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?


回答1:


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)




回答2:


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:

  • How to automate Outlook and Word by using Visual C# .NET to create a pre-populated e-mail message that can be edited
  • How to use Visual C# to automate a running instance of an Office program



回答3:


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();
}



回答4:


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...



来源:https://stackoverflow.com/questions/27421967/how-to-run-outlook-using-process-startoutlook-exe-and-get-the-control-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!