You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中Microsoft.Office.Interop.Word实例化及程序集加载异常求助

修复Microsoft.Office.Interop.Word 15.0.4797.1004的实例化与程序集加载问题

1. 核对Interop程序集引用配置

  • 卸载项目中现有不匹配的Office Interop相关NuGet包,重新安装15.0.x版本的Microsoft.Office.Interop.Word包,确保关联的office程序集版本为15.0.0.0。
  • 右键项目引用中的Microsoft.Office.Interop.Word和office,将嵌入互操作类型属性设置为False,规避类型映射冲突。

2. 确保本地Office环境兼容

  • 必须安装完整版本的Office 2013(对应Interop版本15.0),需包含Word组件和.NET互操作支持模块,禁止使用精简版、绿色版Office。
  • 匹配项目平台目标与Office位数:打开项目属性→生成→平台目标,选择x86或x64,确保和本地安装的Office位数完全一致。

3. 修正代码与程序集绑定

  • 使用显式命名空间别名避免类型混淆:
    using WordInterop = Microsoft.Office.Interop.Word;
    
    // 实例化时明确指定类型
    WordInterop.Application wordApp = new WordInterop.Application();
    
  • 在项目的app.config(桌面程序)或web.config(Web程序)中添加程序集绑定重定向,强制指向正确版本:
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="office" publicKeyToken="71e9bce111e9429c" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-15.0.0.0" newVersion="15.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="Microsoft.Office.Interop.Word" publicKeyToken="71e9bce111e9429c" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-15.0.4797.1004" newVersion="15.0.4797.1004" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>
    

4. 清理重建项目

  • 删除项目目录下的bin和obj文件夹,执行解决方案清理(菜单→项目→清理),再重新生成解决方案,清除旧的编译缓存。

内容的提问来源于stack exchange,提问作者Strelka M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 01:03:17