none
I can't access an Outlook 2010 application from code RRS feed

  • Question

  • Hello.

    In my code, I want to get an object representing Outlook 2010.

    It seems that I am able to start-up a new version of Outlook 2010 without receiving an error. I use this code to start it up:

    oApp = new Microsoft.Office.Interop.Outlook.Application();

    However, if Outlook 2010 is already running, I can't get a version of that application to work with. I use this code to try and get the running Outlook 2010 application:

    oApp = Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;


    Using the latter code line outputs an error/exception:


    Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)) 



    What does this error mean? How can I solve it? I'm running this on a Windows 7 machine.

    Any help would be appreciated. Thanks.
    Monday, September 20, 2010 12:50 PM

Answers

  • Do the security contexts of Outlook and your app match?

    Are they running as teh same Windows user? Is one of them launched using "Run AS Admin"?

    What happens if you turn UAC off?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    • Marked as answer by kshto Tuesday, September 21, 2010 1:26 PM
    Monday, September 20, 2010 4:25 PM
  • That would explain it....

    Can you try to run the app standalone (no VS IDE) and turn off "Run as admin"?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    • Marked as answer by kshto Tuesday, September 21, 2010 1:25 PM
    Monday, September 20, 2010 5:50 PM

All replies

  • Use "new" - Outlook is a singleton, there is always only a single instance of outlook.exe running.
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Monday, September 20, 2010 2:36 PM
  • Hi Dmitry.

    What you are saying works fine if Outlook 2010 is not started, but if it is already started and i try to create a "new" outlook object, then it will throw an exception. My plan is to get some sort of handler on the already running Outlook 2010 application and use that for my application.

    Is there any other way this can work?

    Monday, September 20, 2010 2:53 PM
  • Does something like this work, assuming that outlookApp is declared at class level as an Outlook.Application object. Note the iteration to make sure Outlook is running and the difference in how the application is marshaled:
     
                    System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("OUTLOOK");
                    int collCount = processes.Length;
                    if (collCount != 0)
                    {
                        // Outlook already running, hook into the Outlook instance
                        outlookApp = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
                    }
     
                    if (outlookApp != null)
                    {
                        canQuit = false;
                    }
                    else
                    {
                        // Outlook not already running, start it
                        outlookApp = new Outlook.Application();
                        canQuit = true;
                    }

    --
    Ken Slovak
    [MVP - Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007.
    Reminder Manager, Extended Reminders, Attachment Options.
    http://www.slovaktech.com/products.htm
     
     
    "kshto" <=?utf-8?B?a3NodG8=?=> wrote in message news:104f1c0c-d095-43e6-9197-1d4aa1da72a2...

    Hi Dmitry.

    What you are saying works fine if Outlook 2010 is not started, but if it is already started and i try to create a "new" outlook object, then it will throw an exception.

    Is there any other way this can work?


    Ken Slovak MVP - Outlook
    Monday, September 20, 2010 3:31 PM
    Moderator
  • If Outlook 2010 is already started, it throws this error when I try to Marshal.GetActiveObject:

    Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))

     

    So I replicated the same piece of code on a Windows XP machine, which also has Outlook 2010, and it works fine. The application can GetActiveObject of Outlook 2010.

     

    Could it be something related to only Windows 7?

    Monday, September 20, 2010 3:39 PM
  • Do the security contexts of Outlook and your app match?

    Are they running as teh same Windows user? Is one of them launched using "Run AS Admin"?

    What happens if you turn UAC off?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    • Marked as answer by kshto Tuesday, September 21, 2010 1:26 PM
    Monday, September 20, 2010 4:25 PM
  • Dmitry,

    They are running under the same Windows user, which is an admin. I am currently debugging the application in Visual Studio, and VS (2010) is under "Run as admin".

    I'm not sure about the security contexts though. Where could I find more info about how to find this out? About the UAC, I'll have an answer tomorrow (I left work for today).

    Thanks for the info so far.

    Monday, September 20, 2010 5:46 PM
  • That would explain it....

    Can you try to run the app standalone (no VS IDE) and turn off "Run as admin"?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    • Marked as answer by kshto Tuesday, September 21, 2010 1:25 PM
    Monday, September 20, 2010 5:50 PM
  • Yeah, it seems that that was the problem. When running both the client and Outlook on the same user level (non-admin) this works.


    Thanks Dmitry.

     

    p.s. we (Soocial) also have a licence for your Redemption product. small world...

    Tuesday, September 21, 2010 1:25 PM
  • Use "new" - Outlook is a singleton, there is always only a single instance of outlook.exe running.
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been

    Dmitry has a point, indeed.

    The only way to this code works every time is through new Outlook.Application(), regarless Outlook already opened... and no change of opened instances count after New, if it were 1... 

     if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
                {
    
                    // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
                    //application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
                    // Outlook is a Singleton
                    application = new Outlook.Application();
                }
                else
                {
    
                    // If not, create a new instance of Outlook and sign in to the default profile.
                    application = new Outlook.Application();
                }
                Outlook.NameSpace nameSpace = application.Session;


    Monday, January 13, 2020 4:53 PM