none
Instantiating Outlook (or any other office app) when it is already running. RRS feed

  • Question

  • Hi,

    I am trying to use C# to access the contact list in Outlook.
    To instantiate Outlook I use:

                Outlook.Application app = new Outlook.ApplicationClass();

    However, this line generates an error if Outlook is running. This is the error I get:

    Creating an instance of the COM component with CLSID {0006F03A-0000-0000-C000-000000000046} from the IClassFactory failed due to the following error: 80010108.

    To make it work, not only do I have to close Outlook, I have to kill the OUTLOOK.exe process manually. Only then the instance is created successfully.

    What am I doing wrong? What's the proper way to instantiate Outlook and then dispose of it, regardless of any other running instance?

    (I'm using Office 2007, VS 2005)

    Thanks,
    B. Farkash.

    Sunday, March 30, 2008 9:47 AM

Answers

  • You can use the Marshal.GetActiveObject method to attach to the existing instance of outlook.


    Code Snippet

                Outlook.Application outlookApp = null;
                bool isOutlookRunning = false;
                Process[] processes = Process.GetProcessesByName("OUTLOOK");
                if (processes.Length > 0)
                {
                    try
                    {
                        outlookApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
                        isOutlookRunning = true;
                    }
                    catch { }
                }

                if (!isOutlookRunning)
                {
                    outlookApp = new Outlook.ApplicationClass();
                }



    Monday, March 31, 2008 4:54 AM

All replies

  • You can use the Marshal.GetActiveObject method to attach to the existing instance of outlook.


    Code Snippet

                Outlook.Application outlookApp = null;
                bool isOutlookRunning = false;
                Process[] processes = Process.GetProcessesByName("OUTLOOK");
                if (processes.Length > 0)
                {
                    try
                    {
                        outlookApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
                        isOutlookRunning = true;
                    }
                    catch { }
                }

                if (!isOutlookRunning)
                {
                    outlookApp = new Outlook.ApplicationClass();
                }



    Monday, March 31, 2008 4:54 AM
  • Thank you Abdel for your reply.
    I tried your code. This generated this error for me:

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

    After looking it up I realized that I have a permission problems, probably because I'm running Vista.
    I restarted outlook, but this time ran it as administrator, and then it worked.

    The strange thing is that I'm using an administrator account and I'm running VS as administrator. Why do I have to manually start every program as
    administrator to work with VS?

    Any Ideas? Maybe this discussion should now be moved to a Vista forum.
    Monday, March 31, 2008 8:50 PM
  • Same problem.  I'm running Windows 7, and my application must run as administrator.

    If Outlook is running under the user account, my application can't attach using Marshal.GetActiveObject.

    If both applications are running as administrator, it works fine.

    Friday, May 11, 2012 2:18 PM