How to be sure Outlook application has been loaded completely (application.StartUp event)

1.9k views Asked by At

My C# application needs to work close to Outlook. I've implemented a method that allows to load Outlook if it is not on yet. After this loading method I need to start working with emails, read Inbox folder etc. . . obviously, all these behaviours must be executed when Outlook is on, in particular if the main Outlook windows is ready.

Looking at the OOM I've found the application Startup event, and I think I could use it to be sure the application is ready...but I don't know how to use it.

To share the idea of what I'm trying to do, here there is the code (simplified):

Main:

 OutlookProvider p= new OutlookProvider();
 p.Connect();
 if(p.AppIsOn) {
  // TO DO: start working
 }
 else
    throw new Exception("Error; Unable to connect to Outlook.");

OutlookProvider class:

 #region Fields
   Outlook.Application oApp;
   Outlook.MailItem oMail;
   bool AppIsOn;
 #endregion

 OutlookProvider() { AppIsOn= false; }

 Connect() {
   try {
     // try to connect to the possible running Outlook instance
     oApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
     AppIsOn= true;
   }
   catch(Exception exc) {
    // Outlook is not running, so I create my own Outlook instance

    // here my app is null so an Exception will be thrown
    oApp.Startup += new Outlook.ApplicationEvents_11_StartupEventHandler(SetAppIsOn);
    oApp= (Outlook.Application)new Outlook.Application();
   }
  }

  void SetAppIsOn() { AppIsOn= true;}

Can I use that event to solve my problem? And if yes how can I implement the Connect() method in order to set my boolean AppIsOn variable?

2

There are 2 answers

3
Dmitry Streblechenko On BEST ANSWER

To make sure Outlook is fully initialized, call Namespace.Logon. If Outlook is already running, the call will do nothing.

There is also no reason to call GetActiveObject - Outlook is a singleton, so creating a new object will return the existing object if Outlook is already running.

olApp = new Outlook.Application();
Outlook.Namespace ns = olApp.GetNamespace("MAPI");
ns.Logon();
0
Eugene Astafiev On

First of all, you ned to create an instance of the object and only then try to subscribe to the events. You can't set up an event handler when the object is null (not yet initialized). So, the code should look like the following one:

oApp= (Outlook.Application)new Outlook.Application();
oApp.Startup += new Outlook.ApplicationEvents_11_StartupEventHandler(SetAppIsOn);

Anyway, there is no need to handle the Startup event if you automate the host application. Take a look at the C# app automates Outlook (CSAutomateOutlook) sample application which explains how to automate Outlook from C#. The Outlook object model is not asynchronous, so each method or property will take as much time as it needs.

P.S. There is no method to initialize Outlook. The Logon method is only used to log on to a specific profile when Outlook is not already running. If Outlook is not running and you only want to start Outlook with the default profile, do not use the Logon method.