Search

Sunday, March 16, 2008

How to Check whether Outlook is Running Programmatically in .Net C# ?

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


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:

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;

}

2 comments:

Unknown said...

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

Priyanka said...

Thank u for post. it works

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google