3-steps to programmatically restart #Outlook using #CSharp code

Outlook and other office applications provides an easy way to restart itself. Though there are no directly exposed APIs to restart it, but you can easily write code to do the same operation. Today in this small blog post, we are going to discuss on the code to be implemented.


Just three simple steps and your code is ready. C# Code has been shared at the end of the post. Let me know if you have any queries. Would be happy to help.


3 - steps to restart outlook using C# code (www.kunal-chowdhury.com)


First step is to find and close the running Outlook process. Let us first create a static method called RestartOutlook(). Within this method implementation, first check whether any instance of Outlook is running in the process. You can get the count by calling Count() on top of Process.GetProcessesByName API method, which takes application name as the parameter. This is basic .NET/C# code.


If you find the process running, get the application instance of it and call the Quit() method to close the application. The associated Outlook session will be closed completely and the user will be logged out of the messaging system. Any changes to items not already saved will be discarded.


Second step is a single line to move the running thread in sleep mode for a few milliseconds. This will ensure that, the associated application has been closed completely. In this demo code, I have used 500 milliseconds before running the instance once again.


private static void RestartOutlook()
    if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
        // Check whether there is an Outlook process running
        // If so, use the GetActiveObject method to obtain the application process
         var app = Marshal.GetActiveObject("Outlook.Application") as Outlook._Application;
        // use the Quit method to close the running instance
     Thread.Sleep(500); // Put a few milliseconds delay before launching the app again
    // Create a new process to restart Outlook
     Process process = new Process();
     process.StartInfo = new ProcessStartInfo(OutlookFilepath);


As a third step, create a new process of the outlook app by passing the file path of the application. Calling the Start() method of the process will run the application. This way you can actually restart outlook and/or any office application.


I hope that the small tips was helpful. You can easily restart any office application now, programmatically, using C# code. You can also convert this piece of code in VB.Net or other supported language.


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.


© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top