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

前端 未结 5 861
难免孤独
难免孤独 2021-01-26 15:42

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 w

5条回答
  •  野性不改
    2021-01-26 16:07

    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)

提交回复
热议问题