Check whether outlook is opened or not programmatically? How to know whether outlook is minimized, maximized or closed state in C# the only intuitive way i got is
try { Outlook.Application app = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; } catch (Exception ex) { MessageBox.Show("Outlook is Closed"); } To detect if Outlook is Maximized or Minimized, you can use switch statement on Application.ActiveExplorer().WindowState. Codes: switch(app.ActiveExplorer().WindowState) { case Microsoft.Office.Interop.Outlook.OlWindowState.olMaximized: MessageBox.Show("Maximized"); break; case Microsoft.Office.Interop.Outlook.OlWindowState.olMinimized: MessageBox.Show("Minimized"); break; default: break; }
You can use System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application") to detect if Outlook is closed.If this statement throws an exception, it means Outlook is closed. So Codes are like these:
2 comments:
///
/// Determine whether Outlook is already a running process. Used to automatically start Outlook if this returns false.
///
///
private bool bIsOutlookRunning()
{
try
{
System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("Outlook");
if (p.Length == 0)
return false;
else
return true;
}
catch (Exception ex)
{
Class1 cls1 = new Class1();
cls1.SendError("Senske.SenskePro.clsInvoices.EMailInvoice()", ex, "");
throw (ex);
}
}
// if MS Outlook isn't already running in user context, start an instance.
if (!bIsOutlookRunning())
{
System.Diagnostics.Process.Start("OUTLOOK.EXE");
System.Threading.Thread.Sleep(5000);
}
Thank u for post. it works
Post a Comment