Home > OS >  Loading Interop.Word.dll during runtime
Loading Interop.Word.dll during runtime

Time:03-22

I am currently working on a small console app that should have some sort of integration with Microsoft Word. I guess users might have different versions of Microsoft Office and thats why I decided to load interop asseblies during runtime (however I'm not sure that is right decision).

The next code throws an exception System.ArgumentNullException Value cannot be null. Arg_ParamName_Name. As I understand the problem is WordInterop.GetType("Application") returns null, however I do not understand why.

class Program
{
    static void Main(string[] args)
    {
        Assembly WordInterop = Assembly.LoadFrom("C:\\Windows\\assembly\\GAC_MSIL\\Microsoft.Office.Interop.Word\\15.0.0.0__71e9bce111e9429c\\Microsoft.Office.Interop.Word.dll");
        Assembly Office = Assembly.LoadFrom("C:\\Windows\\assembly\\GAC_MSIL\\office\\15.0.0.0__71e9bce111e9429c\\OFFICE.DLL");

        Type WordApp = WordInterop.GetType("Application");
        dynamic wordAppInst = Activator.CreateInstance(WordApp);
        wordAppInst.Visible = true;

    }
}

CodePudding user response:

The following should work:

var wordAppType = Type.GetTypeFromProgID("Word.Application")
var wordAppInst = Activator.CreateInstance(wordAppType);

CodePudding user response:

As I Found out the problem was in type name. Should be Microsoft.Office.Interop.Word.ApplicationClass instead of Application.

  • Related