开发者

Dynamically load and use COM object in C#

开发者 https://www.devze.com 2023-02-12 08:57 出处:网络
I have a C# project, where I would like to access MS outlook, if it is installed on a client´s machine. The \"access outlook\" part has been done by referencing the outlook COM object, and going from

I have a C# project, where I would like to access MS outlook, if it is installed on a client´s machine. The "access outlook" part has been done by referencing the outlook COM object, and going from there. My problem is now the "if it is installed" part. At the moment, my project doesn´t compile on machines without outlook installed, so I assume that I will have to not reference the outlook component, and instead load and use it dynamically, after detecting that outlook is present, but I haven´t found a way开发者_运维问答 to do this. Am I correct, and does anybody have any hints on how to do this?

Thanks.

Edit: Resolved. Following the advice given by Hans Passant in one of the comments about using the office PIAs, proved to be the path of least resistance. I had a little difficulty getting the PIAs on my office-less machine, but it was overcome using the accepted answer to this question.


You definitely won't be able to compile your assembly on a machine without the Outlook COM object being present, but that doesn't mean that your application will completely fail to work on a machine without Outlook - just that attempting to create or use the Outlook COM object will result in a failure / exception being thrown.

According to this question the best way of detecting whether or not a COM object is present is simply attempting to instantiate it and catching the resulting exception in the case where it is not installed.


Following Hans Passant's advice, I now answer my own question with details of my solution. I was able to both compile and run my project on a computer without office installed. The primary problem was to let visual studio know, which interface it could expect from the COM object. This was solved by finding a primary interface assembly (PIA) file for outlook. That process is better described in this question, but the short story is that I already had the office PIAs on my computer, in this location:

C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA

I use VS2010 pro, I don't know if the files are included with other versions. It was then a matter of copying the relevant file to my project source folder, including it in my project and source control, and making a reference to it. After that, I was able to use Outlook COM types in my project and compile it without error. I am also able to run my project without office installed. This is achieved by trying to instantiate the relevant COM objects, and catching the exception if office is not installed. The following sample code shows how:

using Outlook = Microsoft.Office.Interop.Outlook;

namespace Foo
{
    public class Bar
    {
        public void Quux()
        {
            try
            {
                // try to instantiate outlook COM object.
                Outlook.Application outlookApp = new Outlook.Application();

                // if it works, fine. Proceed
                ...
            }
            // If we catch a COMException, assume no office installed. Deal accordingly.
            catch (System.Runtime.InteropServices.COMException)
            {
                ...
            }
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消