0

"If you want to use Interop, then the component should be installed in the system. Otherwise, it won't work."

I have a WinForm application, that is used by many users in several SO (WinXP, Win7, Win8.1, WinServer 2008, WinServer 2012)

In development, I use a COM component (Outlook, SpeechLib,...).

Two keys:

  1. Some users cannot installed the component. Or they haven't installed it.
  2. Another users can be installed the component.

Any programatically way to:

  • avoid the application fails for the users that hasn't installed the component
  • the application works for the users that has installed the component

?

Notes:

Programmatically way to determine whether a particular COM library DLL has been installed or is installed. Anyways, if a particular COM library not installed, the target is that my source code not fails in runtime in that machine without that COM installed.

For example, for Excel, Word, Outlook COM (ActiveX), SpeechLib (Microsoft Speech Object Library), etc

I could have source code like this:

 SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
    SpeechLib.ISpeechVoice speech = new SpeechLib.SpVoiceClass();
    // ....

Or anyways using Outlool application class, or Excel.Application, etc.

Type officeType = Type.GetTypeFromProgID("Excel.Application");
if (officeType == null)
{
    // Excel is not installed.
    // Show message or alert that Excel is not installed.
}
else
{
    // Excel is installed.
    // Continue your work.
}

My old issue, but not solution:

Detect Outlook installed and load dynamically INterop.Outlook Detect Outlook installed and load dynamically INterop.Outlook

I have a Windows Forms application in VS2010. It has a reference to Interop.Outlook (2003). Then, I have reinstalled Windows XP and VS2010, but not install Outlook.

Now, the project not compiles.

I think this, my application will not work if Outlook not installed in machine that my program executes on.

I need to know if I detect Outlook installed, and load dynamically Interop.Outlook.dll (for using the Outlook PIA or Embedded Interop types in .NET 4).

If the machine has Outlook (2003, 2007, 2010, perhaps need code to detect version and do compatibility of Outlook versions) installed, the application works fine with functionally Outlook.

If the machine hasn’t Outlook installed, (in runtime) the application works fine without functionally Outlook. I think, If the machine hasn’t Outlook installed, (in runtime) the application fails because references (in source code) to Outlook.Application class?.

In development machine, the application works because Outlook (and COM) is installed in the machine.

Any sample source code or goog patterns and practices about it??

References

Does this code fails if Office not installed in machine ? How to detect installed version of MS-Office?

http://www.codeproject.com/Tips/679027/How-to-Check-Whether-Excel-is-Installed-in-the-Sys?msg=5027820#xx5027820xx How to check, programatically, if MS Excel exists on a pc?

http://codeblog.jonskeet.uk/2009/07/07/faking-com-to-fool-the-c-compiler

Community
  • 1
  • 1
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
  • Where is the question? It seems you have already found the solution with the Type.GetTypeFromProgID method. – Eugene Astafiev Mar 26 '15 at 17:19
  • @EugeneAstafiev ***"If you want to use Microsoft.Office.Interop.Outlook, then the component should be installed in the system. Otherwise, it won't work."*** I compile my assembly with this code `olkApp1 = new Microsoft.Office.Interop.Outlook.Application(); olkMail1 = (MailItem)olkApp1.CreateItem(OlItemType.olMailItem);` _When assembly is loaded, not fails if COM not installed?_ – Kiquenet Dec 26 '15 at 12:20

1 Answers1

-1

I posted this elsewhere, but here's the code to detect if Outlook is installed. Basically, it tries to get the Outlook automation object.

using System;
using Microsoft.Office.Interop.Outlook;

class Program
{
    static void Main(string[] args)
    {
        var outlookType = Type.GetTypeFromProgID("Outlook.Application");
        if (outlookType == null)
        {
            Console.WriteLine("Not installed.");
        }
        else
        {
            var app = Activator.CreateInstance(outlookType) as Application;
            Console.WriteLine(app.Name);
        }
    }
}

For SpeechLib, I think the right way to detect it is to just try and create a "new SpVoice()" instance wrapped with a try/catch. If it fails, then assume speech is not installed. Again, embedding the interop will allow you to avoid runtime type load issues.

Hope that helps.

pharring
  • 1,991
  • 1
  • 14
  • 10