oleg-shilo / cs-script

C# scripting platform
http://www.cs-script.net
MIT License
1.57k stars 234 forks source link

Problem referencing Microsoft.Office.Interop.Word.dll #302

Closed brunohunziker closed 1 year ago

brunohunziker commented 1 year ago

I migrated a working solution from version 3.9.10 to 4.4.6 but now I get following exception: "error CS0234: The type or namespace name 'Application' does not exist in the namespace 'Microsoft.Office.Interop.Word' (are you missing an assembly reference?)"

I tried different types of .ReferenceAssemblyXXXX without success.

Are Office.Interop-Assemblies no longer supported, because they are based on COM?

brunohunziker commented 1 year ago

My script:

using System;
using System.Windows.Forms;
using Octoiur.Functionalities;
using Microsoft.Office.Interop.Word;

class ScriptCustomized
{
  public void ExecuteScriptOnApplication(Microsoft.Office.Interop.Word.Application application)
  {
    try
    {
      MessageBox.Show("Test");
      application.DefaultWebOptions().UpdateLinksOnSave = false;
    }
    catch
    {
      // Explicitly do nothing
    }
  }
}
oleg-shilo commented 1 year ago

It's hard to tell for sure but it might be the case. The current (and recent) release(s) of CS-SCript are hosted on .NET6 (.NET Core family) thus COM Interop may have some changes introduced in .NET 5+.

It may also be the case of the CLR not loading Microsoft.Office.Interop.Word.Application assembly the same way as it was in .NET Framework.

I suggest you find the Interop solution by experimenting with the Visual Studio project (.NET6). When you find how to generate/import the COM wrapper assembly, you can simply import it in your script with the usual //css_ref.....

BTW, you cAN generate and open VS project from the script with the -vs parameter:

PS C:\Users\oleg.shilo> css -vs ?
-vs <script> | -vs:init [index_of_detected_VS_executable]
    Generates .NET project file and opens it in Visual Studio.
    The path to the Visual Studio executable (devenv.exe) needs to be defined in the environment variable
    `CSSCRIPT_VSEXE`.
    You can let CS-Script to detect installed Visual Studio executable and interactively select the detected
    executable for integrating it with CS-Script by using `-vs:init` option.
    Alternatively, you can even ask to integrate the first detected executable with:
          css -vs:init 0