3-steps to programmatically restart Outlook using C-Sharp code
Outlook and other office applications provides an easy way to restart itself. Though there are no directly exposed APIs to restart it, but y...- Article authored by Kunal Chowdhury on .
Outlook and other office applications provides an easy way to restart itself. Though there are no directly exposed APIs to restart it, but y...- Article authored by Kunal Chowdhury on .
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.
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 app.Quit(); } 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); process.Start(); }
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.
Thank you for visiting our website!
We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.
We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.