【问题标题】:Cannot create instance for an interface while using Microsoft.Office.Interop.Word使用 Microsoft.Office.Interop.Word 时无法为接口创建实例
【发布时间】:2020-06-01 09:05:06
【问题描述】:

我想将 Word 文档转换为 pdf。为此,我使用Microsoft.Office.Interop.Word。我在两种语言 C# 以及 VB.NET 中都使用它

我在运行时从 GAC 加载 Microsoft.Office.Interop.Word。即使我正在获取类并使用 Type,程序集也会在运行时加载。

对于方法,我使用的是MethodInfo

当我尝试使用Activator.CreateInstance 创建实例时出现问题无法为接口创建实例。

我正在使用两个类,即文档和应用程序。但是当我看到他们的定义接口被实现了。

【问题讨论】:

  • 请在问题正文中以文本形式提供代码。代码图片不足以替代。

标签: c# office-interop word-interop


【解决方案1】:

可以添加对互操作库的引用并正常使用它:

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

以上是我推荐的做事方式。不过,要回答您的问题,我怀疑您可以尝试按照自己的方式进行操作:

Type wordApp = assWord.GetType("Microsoft.Office.Interop.Word.ApplicationClass");

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 2011-10-30
    • 2016-09-18
    • 1970-01-01
    相关资源
    最近更新 更多